Comparing strings in IF statements: unrecognised selector sent to instance - objective-c

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
}

Related

-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance

I got an exception that says:
-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance
Is it saying I am trying to access an NSNull object with a key?
Any idea what causes this and how to fix it or debug further?
The way to fix it is to not attempt objectForKeyedSubscript on an NSNull object. (I'm betting you're handling some JSON data and aren't prepared for a NULL value.)
(And apparently objectForKeyedSubscript is what the new array[x] notation translates into.)
(Note that you can test for NSNull by simply comparing with == to [NSNull null], since there's one and only one NSNull object in the app.)
What ever value you are storing, despite what the editor tells you, at run time you are storing an NSNull, and later on trying to call objectForKeyedSubscript. I am guessing this happening on what is expected to be an NSDictionary. Some thing like:
NSString *str = dict[#"SomeKey"]
Either a piece of code beforehand is not doing its job and investigate there, or perform some validation:
NSDictionary *dict = ...;
if ( [dict isKindOfClass:[NSDictionary class]] ) {
// handle the dictionary
}
else {
// some kind of error, handle appropriately
}
I often have this kind of scenario when dealing with error messages from networking operations.
I suggest adding a category to NSNull to handle this in the same way you would expect a subscript call to be handled if it it were sent to nil.
#implementation NSNull (Additions)
- (NSObject*)objectForKeyedSubscript:(id<NSCopying>)key {
return nil;
}
- (NSObject*)objectAtIndexedSubscript:(NSUInteger)idx {
return nil;
}
#end
A simple way to test is like this:
id n = [NSNull null];
n[#""];
n[0];
With this category, this test should be handled successfully/softly.

iOS - exception when checking if string is empty

I am doing something like this:
// GET THE USER ID
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *user_id = [standardUserDefaults objectForKey:#"user_id"];
And then checking whether the user_id is empty
if ([user_id length] == 0) {
proceed = false;
NSLog(#"Error: User id is not set.");
}
And I get this runtime exception:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean length]: unrecognized selector sent to instance 0x1469f70'
Any idea why I get the exception? I didn't think there was anything too wrong with what I was doing.
Thanks!
This:
NSString *user_id = [standardUserDefaults objectForKey:#"user_id"];
Is returning an NSNumber (as NSCFBoolean is a private subclass of NSNumber) rather than a string. It therefore doesn't implement length, causing the exception.
Perhaps you want to test [user_id intValue] > 0? Even if you convert it to a string it'll always have some length.
(side issues raised: merely declaring user_id as a reference to an NSString doesn't mean that anything you assign to it magically becomes a string; indeed there are no type object-type coercion effects whatsoever. The compiler doesn't complain because the NSUserDefaults return objects of type id, i.e. it guarantees they're objects but makes no claims as to their type, and the compiler doesn't know either. All objects may be cast to and from id without generating a warning, so that it can be used by classes such as the user defaults, NSArray, etc, where they can accept anything as long as it's an object).
EDIT: based on issues raised in the comments, it sounds like the thing originally being stored may not be a string. A good way to validate web stuff is probably something like:
// fall through here if the object isn't a string
if(![responseString isKindOfClass:[NSString class]])
{
// check whether it's something that can be converted to a string
if(![responseString respondsToSelector:#selector(stringValue)])
{
// some sort of error condition; the server returned something
// that isn't a string and doesn't convert (in and of itself) to string
}
else
{
// convert the object to a string
responseString = [responseString stringValue];
}
}
The reason you are getting that error is you are trying to call 'length' on what appears to be a boolean. Either way, for checking if a string is blank here are some easy methods you can add to the NSString class by means of a category:
-(BOOL)isBlank{
return [[self trim] length]==0;
}
-(NSString *)trim{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
}
Then to call it it's just:
[myString isBlank];

Why doesn't this rudimentary Objective C-block code work?

I am trying to understand the fundamentals of blocks. I wrote this simple test:
NSString *(^print_block) () = ^ (NSString *returned_string){
return #"this block worked!";
};
NSLog(#"%#", print_block);
I expected console output to be "this block worked!", but instead I get a big flood of error numbers and etc,, ending with:
terminate called throwing an exception
What up?
Edit: the answer has been suggested to use:
NSLog (#"%#", print_block());
But that doesn't work either. The program terminates at the start of the block definition, with the console saying only (lldb) and Xcode putting a little green arrow at the block definition. The arrow reads:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x5f646e71)
I've tried something else that doesn't work:
NSString *(^print_block) () = ^ (NSString *returned_string){
NSString *return_me = #"this block worked!";
return return_me;
};
NSLog(#"%#", print_block);
But at least this doesn't terminate the program. It runs fine. But the console output is still wrong:
<__NSGlobalBlock__: 0x5a58>
Vatev's comment is right on. When you write:
NSLog(#"%#", print_block);
you're passing the block print_block as the argument for the format string in the log statement. You're trying to print the block. This probably results in [print_block description] being called. I don't know if blocks implement a -description method, but if not then you'll get an unrecognized selector exception.
Also, the way you've declared the block is incorrect. You don't need to include the return value in the parameter list.
The following code works as you expect:
NSString *(^print_block)() = ^{
return #"this block worked!";
};
NSLog(#"%#", print_block());

Why is an NSString 'forgetting' what it is?

I was hoping for some help to understand why I need to re-cast my variable when it is a string from the start.
Here's the code:
+ (BOOL)hasOperandComponents:(NSString *)operandToTest
{
NSArray *componentsOfOperand = [[NSString stringWithFormat:#"%#",operandToTest] componentsSeparatedByString:#" "];
if (componentsOfOperand.count>1) return YES; return NO;
}
If I don't use the embedded call to 'stringWithFormat' then I get the rather common error:
-[__NSCFNumber componentsSeparatedByString:]: unrecognized selector sent to instance
I have been able to find answers to what this error means and hence how to avoid it (see my code above) by searching other Q's and A's... but no good explanation as to why my operandToTest seems to 'forget' that it is a NSString and become an _NSCFNumber.
I'm suspicious that it is because this is a Class method... but why would that matter when a specific instance of NSString *operandToTest is passed to the class method?
Please help?
The problem is you don't have a string to begin with. The value that you're passing into the method as operandToTest is, in fact, an NSNumber*. You need to look at the calling function to figure out why this is.

NSInvalidArgumentException... how do I define the arguments correctly?

I'm getting this exception on the following code. I think it's because I have not defined the two incoming parameter types. They are local; so how do I define them (and where).
Error: 2011-04-27 11:18:03.226
PointPeek[174:707] * Terminating app
due to uncaught exception
'NSInvalidArgumentException', reason:
'+[SQLiteDB addRecordToDatabase::]:
unrecognized selector sent to class
0x1fe70'
Here's the calling line of code:
[SQLiteDB addRecordToDatabase:
symbol.data: symbol.typeName];
and here's the method I'm calling:
- (void) addRecordToDatabase:data: typeName {
NSString *insertCommand = [NSString stringWithFormat:#"INSERT INTO CardData (CARD_ID, CARD_NAME, CODE_VAL) VALUES ('/%#', '/%#', '/%#')", data, #"Test Card", typeName];
if(sqlite3_open_v2(cDatabasePath, &db, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK) {
}
Error: 2011-04-27 11:18:03.226
PointPeek[174:707] * Terminating app
due to uncaught exception
'NSInvalidArgumentException', reason:
'+[SQLiteDB addRecordToDatabase::]:
unrecognized selector sent to class
0x1fe70'
Basically, the "unrecognized selector sent to..." message means you tried to tell an object (or class) to do something it doesn't know how to do. ("selector" is another name for method).
You defined your method of the SQLiteDB class as an instance method:
- (void) addRecordToDatabase:data: typeName;
We know that because of the - in the method name (see Methods and Messaging and Class Interface). In the error message you got, notice that it began with a +, which means you attempted to call a method on the SQLiteDB class itself, rather than on an instance of that class.
In other words, you attempted to do this:
[SQLiteDB addRecordToDatabase: symbol.data: symbol.typeName];
when you needed to do something like this:
SQLiteDB *db = [[[SQLiteDB alloc] init] autorelease]; // an instance
[db addRecordToDatabase: symbol.data: symbol.typeName];
(Note that the previous 2 lines of code aren't all that useful in and of themselves. Presumably, instead of creating an instance of SQLiteDB in this method, you'd have it as an instance variable).
[SQLiteDB addRecordToDatabase: symbol.data: symbol.typeName];
That'd assume that addRecordToDabase:: is a class method, not an instance method.
Furthermore, that is an awful name for a method. Try something like:
- (void)addRecordWithData:(NSData*)aData andType:(NSString*)aType;
That is, bare :s are to be avoided and you should always specify the type of the parameter (and not fall back to id as you did here).
Finally, why aren't you using Core Data or, at the very least, FMDB? Raw SQLite is a waste of time.
SQLite is harder to write code for than Core Data, most likely. If you are a newbie to both, Core Data is a better return on investment of your time.
In any case, the questions in your comment indicate that you really need to start by understanding Objective-C. Apple provides an excellent language guide.