Need formatting help for isEqualToString args - objective-c

Code sample:
NSString *title = [[NSString alloc]initWithFormat: #"%#", [self.answers valueForKey:idVor]];
NSString *message = [[NSString alloc]initWithFormat: #"%#",nameVor];
NSLog(#"%#", title);
NSLog(#"%#", message);
if([title isEqualToString:message])
NSLog(#"equal");
The vars title and message never respond to the if statement even though they contain the same string.
I ran NSLogs for these to see what was contained in each string var.
I got the following output:
f[Session started at 2009-09-21 17:27:56 -0500.]
2009-09-21 17:28:00.256 pickerReview[2394:20b] (
Amedee
)
2009-09-21 17:28:00.257 pickerReview[2394:20b] Amedee
I guess it's not equal because the NSString title var has parentheses around it... Is there a way to format this so that it satisfies the expression in the if statement?

The issue appears to be that you're asking self.answers (an NSArray) for its valueForKey:#"whatever" — this doesn't return a string, but an array made up of the result of asking each object in the array for that key value. NSArray's description method (what gets printed when you NSLog it) is the contents of the array surrounded by parentheses. So you're comparing a string to an array containing a string.

Related

Having trouble taking an index of an array and making it an NSString

I get an array from a JSON and I parse it into an NSMutableArray (this part is correct and working). I now want to take that array and print the first object to a Label. Here is my code:
NSDictionary *title = [[dictionary objectForKey:#"title"] objectAtIndex:2];
arrayLabel = [title objectForKey:#"label"];
NSLog(#"arrayLabel = %#", arrayLabel); // Returns correct
//Here is where I need help
string = [arrayLabel objectAtIndex:1]; //I do not get the first label (App crashes)
NSLog(#"string = %#", string);
other things that I have already tried are as follows:
string = [NSString stringWithFormat:#"%#", [arrayImage objectAtIndex:1]];
and
string = [[NSString alloc] initWithFormat:#"%#", [arrayImage objectAtIndex:1]];
Any help is greatly appriciated!
EDIT: The app does not return a single value and crashes.
Your code doesn't match the structure of your JSON. In your comment on the deleted answer, you said you got an exception when sending objectAtIndex: to an NSString. In your case, arrayLabel isn't an array when you think it is.
If your JSON has an object, your code needs to treat it as an NSDictionary. Likewise for arrays and NSArray and strings and NSString.
In addition to whatever else was going on, you repeatedly refer to "first" but use the index 1. In most C-based programming languages (and others, as well) the convention is that indexes into arrays are 0-based. So, use index 0 to get the first element.

Got an error when try to print stringByReplacingOccurrencesOfString method

When i try to print this string i got bad access error:
NSString *myPath = [myPath stringByReplacingOccurrencesOfString:#"/Users/Me/Library/iPhone/4.2/MyApp/Documents/Photos/pic1.png"
withString:#"/Users/John/Library/iPhone/5/MyApp/Documents/Photos/picture.png"];
NSLog(#"%#", myPath);
Why?
Thanks.
The error is because you are calling the method stringByReplacingOccurrencesOfString:withString on a variable (myPath) that has not been instantiated. You need to call that method on an instance of the NSString class that already contains the string you are replacing text in.
When you call a method you call it on the receiver. Therefore you are calling stringByReplacingOccurrencesOfString:withString: on myPath.
You are assigning the value of the method into
NSString *myPath
which makes me assume that the myPath in
[myPath ....
is not actually set to anything. (potentially pointing to garbage)
What you want is something like this
NSString *startString = #"hello";
// Receiver Message
// | |
// v v
NSString *replacedString = [startString stringByReplacingOccurrencesOfString:#"hello"
withString:#"bye bye"];
NSLog(#"Results in => %#", replacedString);
// Output
2011-12-11 20:50:01.964 Untitled 2[779:707] Results in => bye bye
In your above comment you tried NSString *myPath = [[NSString alloc] init]; this would create an empty string. An empty string does not contain any occurrences of #"/Users/Me/Library/iPhone/4.2/MyApp/Documents/Photos/pic1.png" therefore it can't replace them.

\n does not skip to next line in NSString

NSMutableString *a = #"Hi";
NSMutableString *b =[a stringByAppendingString:#"\n\n Hi Again"];
The above doesn't give an error but does not put "Hi Again" on the next line. Why?
EDIT2
I realised after posting, that the OP had NSString in the title but put NSMutableString in the code. I have submitted an edit to change the NSMutableString to NSString.
I will leave this as it still maybe helpful.
Well I am surprised that does not give an error, because you are giving a NSMutableString a NSString.
You need to read the Documentation on NSMutableStrings.
to give you an idea
//non mutable strings
NSString *shortGreetingString = #"Hi";
NSString *longGreetingString = #"Hi Again";
/*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/
NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];
/*The mutableString, now uses an appendFormat to construct the string
each %# in the Parameters for the appendFormat is a place holder for values of NSStrings
listed in the order you want after the comma.
Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:#"%#\n\n%#",shortGreetingString,longGreetingString];
NSLog (#"mutableString = %#" ,mutableString);
[pool drain];
I think this might help you. You'd rather to use '\r' instead of '\n'
I also had a similar problem and found \n works in LLDB but not in GDB
Try using NSString. You could use:
NSString *a = [NSString stringWithFormat:#"%#\n\n%#", #"Hi", #"Hello again"]
If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0
myView.numberOfLines=0;

Concatenating a string with the value of a NSTextField results in odd symbols in Objective-C

I'm in my second day of learning Objective-C, Cocoa and IB. This is probably something really simple but I cannot work it out.
Basically, I have a form with a NSTextField, when the user types in this field and clicks an OK button the application will display an alert saying Hello followed by the value of text field.
It's all working apart from the string concatenation. I'm using the following code to concatenate the string "Hello" and the NSTextField value:
NSString *nameText = [NSString stringWithFormat:#"Hello %s", [nameTextField stringValue]];
When the user clicks the OK button an alert displays "Hello ‡√Ÿpˇ"!
Camsoft,
The NSString of Obj-C is an object, correct the format call with:
NSString *nameText = [NSString stringWithFormat:#"Hello %#",[nameTextField stringValue]];
Note the %# instead of %s .
Frank
stringValue returns NSString object and %s expects c-string parameter. Try to use %# instead:
NSString *nameText = [NSString stringWithFormat:#"Hello %#", [nameTextField stringValue]];

stringByAppendingFormat not working

I have an NSString and fail to apply the following statement:
NSString *myString = #"some text";
[myString stringByAppendingFormat:#"some text = %d", 3];
no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString.
any clues most welcome.
I would suggest correcting to (documentation):
NSString *myString = #"some text";
myString = [myString stringByAppendingFormat:#" = %d", 3];
From the docs:
Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.
It's working, you're just ignoring the return value, which is the string with the appended format. (See the docs.) You can't modify an NSString — to modify an NSMutableString, use -appendFormat: instead.
Of course, in your toy example, you could shorten it to this:
NSString *myString = [NSString stringWithFormat:#"some text = %d", 3];
However, it's likely that you need to append a format string to an existing string created elsewhere. In that case, and particularly if you're appending multiple parts, it's good to think about and balance the pros and cons of using a mutable string or several immutable, autoreleased strings.
Creating strings with #"" always results in immutable strings. If you want to create a new NSMutableString do it as following.
NSMutableString *myString = [NSMutableString stringWithString:#"some text"];
[myString appendFormat:#"some text = %d", 3];
I had a similar warning message while appending a localized string. This is how I resolved it
NSString *msgBody = [msgBody stringByAppendingFormat:#"%#",NSLocalizedString(#"LOCALSTRINGMSG",#"Message Body")];