为什么我没有从NSStringFromClass获取NSString?

时间:2012-07-21 13:39:06

标签: ios

你会认为这会返回一个NSString:

NSString *class = NSStringFromClass([[_array objectAtIndex:([_array count] -2)] class]);

NSLog(@"Is a kind of NSString: %@", ([[_class classForCoder] isSubclassOfClass:[NSString class]])? @"Yes" : @"No");    

if ([class isEqualToString:@"MapViewController"]) {
    [_appDelegate.navController0 pushViewController:_userListController animated:YES];

} else {
    [_appDelegate.navController3 pushViewController:_userListController animated:YES];
}

但由于某种原因它没有,导致这次崩溃:

Is a kind of NSString: No
-[NSKeyValueObservationInfo isEqualToString:]: unrecognized selector sent to instance 0x6e69080

即使我尝试这个:

NSString *class = (NSString *)[[_array objectAtIndex:([_array count] -2)] class];

它仍然没有返回NSString,导致崩溃:

Is a kind of NSString: No
+[MapViewController isEqualToString:]: unrecognized selector sent to class 0xd4460

我知道进去了,最后一个例子有点延伸,但是我很无能为力,为什么我没有从任何一个返回NSString?

1 个答案:

答案 0 :(得分:2)

当我从一个方法外部访问NSString“class”时,我会得到各种古怪的错误。所以我所做的是添加一点保留声明:

NSString *class = [NSStringFromClass([[_array objectAtIndex:([_array count] -2)] class]) retain];

现在它的工作就像一个魅力。