I have the lines
usernameString = [[NSString alloc] initWithFormat:[username text]];
[username setText:usernameString]; // <--- warning Format not a string literal and no format arguments
username is a UITextField
Tell me if you need anymore info
If you just want to duplicate the string, you can do that using initWithString:, which gives you back a new string with the same contents as the string passed in:
usernameString = [[NSString alloc] initWithString:[username text]];
Try:
usernameString = [[NSString alloc] initWithFormat:#"%#",[username text]];
Don't forget to release usernameString later, if you need to.
Related
Im trying to read some values from a local url when i press a button and i send the value to a label. as soon as i launch the app and i press the button for the first time i get the value. but the second time i press the button the label get no text even though the NSLog message continues to display the value. any idea?
here is the code:
- (IBAction)refresh:(id)sender {
NSArray *listItems;
NSString *msg;
NSStringEncoding encoding;
NSURL *url = [NSURL URLWithString:#"http://192.168.1.177"];
NSData *data = [NSData dataWithContentsOfURL:url];
msg = [[NSString alloc] initWithData:data encoding:encoding];
listItems = [msg componentsSeparatedByString:#" "];
label.text = listItems[0];
NSLog(listItems[0]);
}
Your encoding variable is never initialized with a value, so it has the default value of 0, which is not a valid value of NSStringEncoding according to NSString.h. If you run the static analyzer (Command-Shift-B in Xcode), you will get a message that has an uninitialized value when it is used.
Change this line:
msg = [[NSString alloc] initWithData:data encoding:encoding];
to this:
msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
(Substitute the appropriate encoding, but NSUTF8StringEncoding is a good first guess.)
You should also use a format string to log:
NSLog(#"%#", listItems[0]);
I want my output to be "23", but it still shows "123".
NSString *valorTextField = [[NSString alloc] initWithFormat:#"123"];
[valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", valorTextField);
stringByReplacingOccurrencesOfString returns a new string:
NSString *valorTextField = [[NSString alloc] initWithFormat:#"123"];
NSString *replaced = [valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", replaced);
Alternatively, you can work with a NSMutableString and use the
replaceOccurrencesOfString:withString:options:range: method.
You are not assigning returned string to valorTextField again. You are just executing function. Actual string is unchanged. Instead write:
valorTextField = [valorTextField stringByReplacingOccurrencesOfString:#"1" withString:#""];
NSLog(#"%#", valorTextField);
In my app, I have a view where user have to fill a form. But, sometime the app crash here, in this function, that simple cacth the value field and built a url to give
-(NSString*)urlToUpload{
NSString *string1 =[[NSString alloc]init];
string1= [NSString stringWithFormat:#"?nombre="];
NSString *string2 = [string1 stringByAppendingString:nameAdded];
//crash here
NSString *string3 = [string2 stringByAppendingString:#"&horario="];
NSString *string4 = [string3 stringByAppendingString:horarioAdded];
NSString *string5 = [string4 stringByAppendingString:#"&info="];
NSString *string6 = [string5 stringByAppendingString:infoAdded];
NSString *string7 = [string6 stringByAppendingString:#"&offerta="];
NSString *string8 = [string7 stringByAppendingString:offertaAdded];
NSString *lat_string = [[[NSString alloc] initWithFormat:#"%f",locationToUpload2.latitude] autorelease];
NSString *lon_string = [[[NSString alloc] initWithFormat:#"%f",locationToUpload2.longitude] autorelease];
NSString *string9 = [string8 stringByAppendingString:#"&latitude="];
NSString *string10 = [string9 stringByAppendingString:lat_string];
NSString *string11 = [string10 stringByAppendingString:#"&longitude="];
NSString *string12 = [string11 stringByAppendingString:lon_string];
NSString *url1 = [NSString stringWithFormat:#"http://myserverside/mysql_up.php"];
NSString *url = [url1 stringByAppendingString:string12];
return url;
}
EDIT:
It seems problem appers on nameAdded when there is a white space into textField(i.e. MisterB not crash, Mister B yes ).
But I am using:
nameAdded =[[nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and NSLOg give of nameAdded is Mister%20B.
The crash still appearing...
Just use a single stringWithFormat::
- (NSString *)urlToUpload {
NSString *url = [NSString stringWithFormat:#"http://myserverside/mysql_up.php?nombre=%#&horario=%#&info=%#&offerta=%#&latitude=%f&longitude=%f",
[nameAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[horarioAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[infoAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[offertaAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
locationToUpload2.latitude, locationToUpload2.longitude];
return url;
}
Make sure the referenced variables are valid.
In your original code there is no need to alloc/init an NSString then assign another string to the same variable. That's a memory leak (string1).
If you really want to structure your code the way you have it, at least use an NSMutableString and append to that one mutable string. Creating over a dozen NSString variables is the wrong way to do it.
Updated: Ensure each of the strings added to the URL are properly escaped.
It looks like nameAdded may be the cause of your problems. Is it nil at that point?
Also
You are allocating a string, setting it to string1 and then immediately setting string1 to the class function stringWithFormat which allocates another string. Also you are using stringWithFormat but you aren't using any format so you could simply use NSString *string1 = #"?nombre=";
Rather than declaring all of those variables you should just use NSMutableString and build it all in one variabl
char arr[] = "abcdefg";
// I know I can do this
NSString *s = [[NSString alloc] initWithCString:arr encoding:NSUTF8StringEncoding];
If I want to convert part of the arr array into an NSString*? say cde rather then the whole string abcdefg? How do I achieve that?
NSString *s = [[NSString alloc] initWithBytes:arr + 2
length:3 encoding:NSUTF8StringEncoding];
Why is substringFromIndex not working for my NSMutableString ?
Here code similar to what I have :
NSMutableString *myString = [[NSMutableString alloc] initWithString:#"This is my String"];
[myString substringFromIndex:5];
NSLog(#"myString = %#", myString); //will output This is my String
If I use substringFromIndex on NSString it will work, for example like so :
NSString *tempStr = [[NSString alloc] init];
tempStr = [myString substringFromIndex:5];
NSLog(#"tempStr = %#", tempStr); //will output is my String
Why does it not work in the first example, and I have one more question, if I do it using the second method, and then I set:
[myString setString:tempStr];
[tempStr release];
This will result in a crash, I thought, since I used setString on NSMutableString, that I do not need the NSString and I release it, but apparently that is not the case, however if I use autorelease it will be OK
That method never alters the string you call it on. It returns a new string in both cases. So assign it to a new string variable and your good.
It's crashing because you over releasing one object and and leaking another. You alloc the first string, then make a new autoreleased string from substringFromIndex:, then release it. You dont need to try this hard.
Simply assign the output of the substring method to a variable, and let it be autoreleased for you. No alloc, no release.
A full proper example might look like this:
NSMutableString *myString = [[NSMutableString alloc] initWithString:#"This is my String"];
NSString *tmpString = [myString substringFromIndex:5];
NSLog(#"tempStr = %#", tempString);
[myString setString:tempStr];
// later do [myString release]
or even simpler:
NSString *myString = #"This is my String";
myString = [myString substringFromIndex:5];
You create an NSMutableString specifically so you CAN modify it.
But no one knows any way to do this:
[myMutableString substringFromIndex:5];
And if you are going to do this instead:
myMutableString = [myMutableString substringFromIndex:5];
why even use a mutable in the first place.
MutableStrings are great for ADDING (appending) to them.
Not sure why we can't also shorten (substring) them. (Without making a new copy.)
[myString substringFromIndex:5] returns a new NSString that starts from the specified index. It does not modify myString.
try this code instead:
NSLog(#"myString = %#", [myString substringFromIndex:5]);