my code is like this:
(here name is NSArray and object is also array which is passed as function parameter)
[name addObject:object];
NSlog(#"%#",name);
When i do this then i got output like this:
{name:\"Malay Basu"\ date:\"2013-04-18"\ senderId:\"24" receiverId:\"25"}
Now when i am retrieving name field of array into string then i got the string as:
NSString *temp = [name valueForKey:#"name"];
output: `( Malay Basu )`
Now when i do string operations like string appending or comparison of string then i got the error like:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16635ed0'
To remove this error i need this string without bracket as displays above. So i need Malay Basu instead of ( Malay Basu ).So what is my next step to fetch proper string into another string variable because i need to do operations like string compare,string appending. Any help will be appreciated.
Check the purpose of the methods you are using.
-valueForKey: when called on an NSArray
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
So your object isn't a string, it's an array of strings.
The log statement shows brackets because that is the log notation for NSArray. Similarly {} brackets are the log notation for NSDictionary.
To process the result strings, use a loop.
for (NSString *aString in anArray) {
NSLog(#"%# is an %#", aString, NSStringFromClass([aString class]);
}
Related
Could anyone please tell me why I am getting this error and why this code isn't working?
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSArrayI componentsSeparatedByString:]: unrecognized
selector sent to instance 0x109494750'
This is the code with problems:
NSArray *array = [self.managedObjectContext executeFetchRequest:request error:nil];
NSString *dateString = [array valueForKey:#"dateString"];
NSArray *datesArray = [dateString componentsSeparatedByString:#","];//line with problems
When you call valueForKey: on array the result will be an NSArray containing the result of calling valueForKey: on each of it's elements.
So dateString is not actually a string it's an instance of NSArray, which does not respond to componentsSeparatedByString:. You need to index into the array to get the date you want before calling componentsSeparatedByString: on that
It's an indication that dateString isn't a string. When you call -valueForKey: on an array, it returns an array. Per the docs:
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
So you're calling a string method on an array. It's not clear what you're trying to accomplish by calling -valueForKey:. Perhaps you meant -objectAtIndex:?
I have a NSMutablearray wich contain NSArrays(each array contain int and String values).
When i try to retrieve and display the data from the first array:
That was ok with the int value, it was displayed correctly.
NSLog(#"%i",[[[lesQuestions objectAtIndex:0] objectAtIndex:0] intValue]);
But when i try to display the String value:
NSLog(#"%#",[[[lesQuestions objectAtIndex:0] objectAtIndex:1] stringValue]);
I got exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringValue]: unrecognized selector sent to instance
I am definitely sure that the int value is the first item (index 0) and the String value is the second (index 1).
When i log the MutableArray which holds the NSArrays, i got the values correctly, so the problem is on the reference of the item i guess.
The object in your array is already an NSString, so the call to stringValue is unnecessary. NSString doesn't implement a method called stringValue, hence the exception you're seeing. Just do this:
NSLog(#"%#",[[lesQuestions objectAtIndex:0] objectAtIndex:1]);
-intValue is a method implemented by NSNumber, to get an integer primitive out of the NSNumber instance, and your use of intValue is correct assuming the first object in the array is an NSNumber (or an NSString, which also implements -intValue).
All that said, I don't generally think it's a great idea to store instances of different classes in the same array like you're doing. You'd probably be better off using an NSDictionary where each value is stored with a unique key, say #"index" for the number and #"name" for the string.
NSLog respectively the ability of standard objects do display themselfs is more powerful than you may think.
NSLog(#"%#",[[lesQuestions objectAtIndex:0] objectAtIndex:0]);
NSLog(#"%#",[[lesQuestions objectAtIndex:0] objectAtIndex:1]);
You could even find this useful unless you have a really high number of array elements:
NSLog(#"%#",[lesQuestions objectAtIndex:0]);
or
NSLog(#"%#",lesQuestions);
Give it a try!
I'm getting garbage values when manipulating some of my NSString objects. I think the problem stems from my misunderstanding of how NSString works at a basic level. Below, I have a object which has a string pointer as a synthesized property. When I try to log it out directly, the compiler gives me a warning, but it does log out the value I expect. On the very next line, I try to log that string out the proper way but I end up getting garbage.
Code snippet
MyObject *object = [self.objects objectAtIndex:indexPath.row];
NSString *myString = object.myString;
NSLog(myString); // format not a string literal and no formal arguments
NSLog(#"formatted = %s", myString);
Output
2011-05-16 13:06:51.137 MyProgram[917:207] thisValueIsGood
2011-05-16 13:06:51.138 MyProgram[917:207] formatted = `å
This problem has snowballed onto other functions which use this final string. When I concatenate that string with other strings, I get even more garbage.
NSString is an object, instead of %s, use %#.
So i have this block of code it adds players to an NSMutableArray in my ViewController playerList. For some reason i cannot print all the playernames to the log. Am I doing something wrong? I keep getting an error that says member refrence struc objc_object is a pointer. Can anyone see what im doing wrong?
p1,p2,p3,p4 are all NSString Objects that just have the players names.
the addPlayer method creates a new player object with a property named playerName.
- (IBAction)addPlayerButton:(id)sender {
[self.playerList addObject:[self addPlayer:p1]];
[self.playerList addObject:[self addPlayer:p2]];
[self.playerList addObject:[self addPlayer:p3]];
[self.playerList addObject:[self addPlayer:p4]];
for (id element in playerList) {
NSLog(element.playerName);
}
}
for (id element in playerList) {
NSLog(element.playerName);
}
The compiler warning/error is because element is of type id and you can't use the dot syntax with object references of type id (a specific design choice when creating that feature, btw).
Fixed code:
for (Player *element in playerList) {
NSLog(#"%#", element.playerName);
}
Two (unrelated) problems fixed:
explicitly type element to be a reference to your player class (I assumed the name). This'll allow the dot syntax to work.
Use a format string with NSLog. If a player's name were ever to contain a formatting sequence -- %#, for example -- then NSLog() would try to expand the next (non-existent) argument to NSLog and your app would crash or print garbage (say, if the player's name were "Bob %f %f %f").
doesnt look like they are getting
added to the array properly
Make sure you allocate an array and assign it to playerList somewhere:
self.playerList = [NSMutableArray array];
Use this instead:
NSLog(#"%#", element.playerName);
NSLog is sort of like printf() and friends, but not exactly. You must provide a first argument that is a string literal with the format you want to use, followed by any variables represented in the format. In Objective-C, the special format %# means "use the obeject's description method to fill in a value (if there is one)." Sometimes you get a debugger-like output for an object that does not have that method, e.g. or some such, which isn't too useful of course.
In your case, assuming playerName is an NSString, you'll see it's name output if you use the format %# in NSLog's first argument.
EDIT:
You should be able to use a for statement like this:
for(Player *p in playerList) {
NSLog(#"%#", p.playerName);
}
Just because you use addObject: to add the objects doesn't mean you have to give up using the objects' type when you look at them from the array.
If in fact the objects in playerList are just NSStrings, then your loop can simply be
for(NSString *name in playerList) {
NSLog(#"%#", name);
}
in my code, I need to compare two strings to see if they are equal. if they are it needs to preform a function. one of the strings is just a #"someString", the other is part of an object.
if ([[[mine metal] stringValue] isEqualToString:#"Gold"])
{
//some function
}
however there are some complications when I do this. first, it gives me a warning: NSString may not respond to -stringValue. and when I run the Application it quits out at the if statement: the console reports " -[NSCFString stringValue] : unrecognized selector sent to instance." mine.metal is defined through a fast enumeration loop across an array; the metal attribute is defined as an NSString, and NSLog is able to display this string. what else am I missing?
The compiler warning and the subsequent run-time error both tell you what the problem is.
[mine metal] returns an NSString. NSString doesn't have a method called stringValue.
If [mine metal] does indeed return an NSString then you can do this:
if ([[mine metal] isEqualToString:#"Gold"])
{
//some function
}