variable was written but never used in Swift 2.0 & Xcode 7 - xcode

When I create a variable without String() to initialize the variable an error shows up alerting, "variable not initialize", same error shows up when I directly assign
var username: String = usernameTextfieldSigup.text!
And when I initialize the variable using the code below,
var username: String = String()
username = usernameTextfieldSignup.text!
warning comes up, variable 'username' was written but never used.
I know I could just ignore these warnings but I'd like a cleaner code.
I'm using Xcode7 beta5 release.

You can declare the username without a value first:
var username: String
and then assign a value to it in your initializer:
// replace with your (valid) initializer
func init() {
username = usernameTextfieldSignup.text!
}
However, I'm sure that usernameTextfieldSignup.text won't have a value until the user ha actually provided a value. So in that case I would recommend to make username an optional so you can set it's value when you need to:
var username: String?
and then when setting it:
username = usernameTextfieldSignup.text
and when accessing it:
if let uname = username {
// do something with the uname
}
Also, the variable 'username' was written but never used warning you're seeing is probably because you write to / initialise username but there's no code actually reading it, so it's kind of a useless variable according to the compiler then.

To init an empty string you have to either make it explicit or optional like so:
let string: String!
let string: String?
You can do this with any datatype.

Related

Arduino Yun run shell command with variable

I need help on this part of a project I am making, I need to run a shell command and pass a variable either string or char to it.
p.runShellCommand("madplay /mnt/sda1/");
Above is my shell command which works, however I want to put a variable after the last slash
p.runShellCommand("madplay /mnt/sda1/variable");
The above code is what I have tried, replacing variable with my variable and didn't seem to work.
I have also tried this which seems to work
String hey = "madplay /mnt/sda1/worldOfTomorrow.mp3";
p.runShellCommand(hey);
It's just string concatenation:
String command= "madplay /mnt/sda1/";
String var = "worldOfTomorrow.mp3";
p.runShellCommand(command+var);
Now, write in var with desired function:
String command= "madplay /mnt/sda1/";
String var = getNameOfFile();
p.runShellCommand(command+var);
...
public String getNameOfFile(){
//code retrieving your desired variable by Serial/SDcard/Ethernet....
}

can't remove optional() from string after updating to latest xcode?

after updating xcode I simply can't remove the optional() from my string?
retrievedUsername = KeychainWrapper.stringForKey("username")!
this prints out
optional("HK")
but I need it to be
HK
I've tried
if let username = KeychainWrapper.stringForKey("username"){
retrievedUsername = username
}
but no luck!
any ideas?
Your value is probably an optional containing an optional, so you'll have to unwrap it twice:
if let temp = KeychainWrapper.stringForKey("username"), let username = temp {
retrievedUsername = username
}
If this doesn't work, this is because it's not a double optional, and it means that your original string already contains the text "Optional(HK)" due to a prior error.
I found out the problem, it would save to the keychain with "optional" so when it retrieves the string it is "optional("HK")" that's why unwrapping it didn't work

NSLocalizedString should be used directly for exporting XLIFF?

I used to use NSLocalizedString by custom function.
For example, to access Profile.strings, I define this function:
func LocalizedProfile(key: String, comment: String?) {
NSLocalizedString(key, tableName: "Profile", comment: comment ?? "")
}
And, called like this:
let localized = LocalizedProfile("Submit", comment: "For registration")
This method works fine except for exporting XLIFF.
On the Xcode 6.3.2, executting Export for localizationthrows error:
To get error information, I executed via command line:
xcodebuild -exportLocalizations -localizationPath ./xliff -project MyApp.xcodeproj -exportLanguage ja
And, I got this error:
Bad entry in file /Users/mono/Documents/Git/MyApp/Localization.swift (line = 29): Argument is not a literal string.
Defining custom localization method is very useful for me, but I also want to use exporting XLIFF feature.
Are there any methods to resolve this demands?
Export For Localization and xcodebuild -exportLocalizations both generate strings files by looking for invocations of NSLocalizedString(_:tableName:bundle:value:comment:) in code and uses the static values passed into the parameters to create the appropriate strings files.
This means that the only values you can pass into key, comment, value, and tableName are string literals.
Since you're using a wrapper function around NSLocalizedString(_:comment:) to localize your strings, the only time Xcode sees you calling NSLocalizedString(_:comment:) is in that one wrapper function with non-string-literal values, which is invalid.
What you really want to do instead is call NSLocalizedString(_:tableName:comment:) directly.
Alternatively, you can call Bundle.localizedString(forKey:value:table:) in your wrapper function, but then you have to manually create your own strings files for those key-value pairs.
/// - parameter comment: This value is ignored, but should be provided for
/// context at the call site about the requested localized string.
func LocalizedProfile(key: String, comment: String?) -> String {
return Bundle.main.localizedString(forKey: key, value: nil, table: "Profile")
}

How can I convert a session variable to a string array?

I’m trying to store an array in a session variable then use it latter like this:
Session["sessionVariable"] = searchString;
Now here I’m trying to store the session variable into a string variable.
String[] sv = Session["sessionVariable"];
When I do I get his error.
Cannot explicitly convert type ‘Object’ to ‘String[]’, An explicit conversion exists, (are you mission a cast?)
I’ve tried various conversions but can’t find the correct one. Can you please help me to find the correct conversion? Thanks.
Try this:
Set:
Session["test"] = new string[] { "1", "2", "3" };
Get:
string[] array = Session["test"] as string[];

How to create XML object from string using xml-mapping in Ruby

I'm using xml-mapping in Ruby (on Sinatra) for some XML stuff. Generally I follow this tutorial: http://xml-mapping.rubyforge.org/. I can create objects and write them to XML strings using
login.save_to_xml.to_s
But when I try
login = Login.load_from_xml(xml_string)
I get the following error:
XML::MappingError - no value, and no default value: Attribute username not set (XXPathError: path not found: username):
Here is the XML string I receive:
<login><username>ali</username><password>baba</password></login>
This is what the class looks like:
class Login
include XML::Mapping
text_node :username, "username"
text_node :password, "password"
end
So the class name is the same, the nodes are named the same. I actually get the exact same string when I create an instance of my object and fill it with ali/baba:
test = Login.new
test.username = "ali"
test.password = "baba"
p test.save_to_xml.to_s
<login><username>ali</username><password>baba</password></login>
What am I missing?
Thanks,
MrB
EDIT:
When I do
test = login.save_to_xml
And then
login = Login.load_from_xml(test)
it works. So the problem seems to be that I'm passing a string, while the method is expecting.. well, something else. There is definitely a load_from_xml(string) method in the rubydocs, so not sure what to pass here. I guess I need some kind of reverse to_s?
It looks like you save_to_xml creates a REXML::Element. Since that works, you may want to try:
Login.load_from_xml(REXML::Document.new(xml_string).root)
See the section on "choice_node" for a more detailed example http://xml-mapping.rubyforge.org/

Resources