简体   繁体   English

将NSDictionary值(id?)转换为NSString或NSNumber以进行比较

[英]Convert NSDictionary value (id?) to NSString or NSNumber to compare it

pls help me with my trouble: I can't compare to value to know successful result was or not. 请帮助我解决问题:我无法比较价值,知道成功与否。 I fetch json-object as NSDictionaty: 我将json-object作为NSDictionaty获取:

    NSDictionary *returnDictionary = [NSJSONSerialization JSONObjectWithData:responseObject                                                                                                                                       options:0 error:nil];

And after that I get value for key: code 之后我获得了key:code的价值

    [returnDictionary objectForKey:@"code"];

If returning value is equal 1 so that's OK, but problem is that I don't know the type value of key "code". 如果返回值等于1则可以,但问题是我不知道键“代码”的类型值。 I tried to compare id, NSString, NSNumber but all were fail. 我试图比较id,NSString,NSNumber,但都失败了。 What type of object should I compare ? 我应该比较什么类型的对象?

Did you use the comparison operator ( == ) to compare your objects? 您是否使用比较运算符( == )来比较对象?

If so, you didn't compare the values of your objects but their memory addresses. 如果是这样,您没有比较对象的值,而是比较它们的内存地址。
If the object returned by [returnDictionary objectForKey:@"code"] is of type NSString you should use NSString's isEqualToString: If it returns a NSNumber instance, you could compare the intValue of that object to 1. 如果[returnDictionary objectForKey:@"code"]返回的对象是NSString类型,则应使用NSString's isEqualToString:如果它返回NSNumber实例,则可以将该对象的intValue与1进行比较。

[[returnDictionary objectForKey:@"code"] isEqualToString:@"1"]  

or 要么

[[returnDictionary objectForKey:@"code"] intValue] == 1

You can use 您可以使用

[[returnDictionary objectForKey:@"code"] isKindOfClass:[NSString class]]

to check the class of the object pulled from the dictionary. 检查从字典中提取的对象的类。

Hope it helps! 希望能帮助到你!

If you expect the output to be an integer, you can call intValue . 如果您希望输出为整数,则可以调用intValue

[[returnDictionary objectForKey:@"code"] intValue] == 1

Also, you can log the type of the returned object by + class; 此外,您可以按+ class;记录返回对象的+ class; method. 方法。

NSLog(@"%@", [[returnDictionary objectForKey:@"code"] class]);

put in a breakpoint and print out the classname... 放入断点并打印出类名...

type into the debugger: po [[returnDictionary objectForKey:@"code"]className] 输入调试器: po [[returnDictionary objectForKey:@"code"]className]

that will give you the class, something like _NSCFString will be a string etc... 这将给你的类,像_NSCFString之类的东西将是一个字符串等...

id is something of a "cheat" in Objective-C, though one that is officially "blessed". 在Objective-C中, id是一种“作弊”,尽管它是正式的“祝福”。 Generally, where a method returns id you can directly use that value to invoke a method on the returned type, without having to first cast to the appropriate type -- the compiler knows to suppress "can't find that method name" type warnings/errors when the call is on an id . 通常,在方法返回id您可以直接使用该值来调用返回类型的方法,而不必先强制转换为适当的类型 - 编译器知道抑制“找不到该方法名称”类型警告/呼叫在id上时出错。

Of course, if you don't know the class of the object, you don't know whose methods to call. 当然,如果你不知道对象的类,你不知道调用哪个方法。 As Pablo suggests you can use isKindOfClass to test, if you have a suspicion. 正如Pablo建议您可以使用isKindOfClass进行测试,如果您有疑问。 Alternatively, you can log the class name of an object with NSLog(@"The class is %s", object_getClassName(someId)); 或者,您可以使用NSLog(@"The class is %s", object_getClassName(someId));记录对象的类名NSLog(@"The class is %s", object_getClassName(someId)); or one or two other ways. 或一两种其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM