0

你会认为这会返回一个 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 ?

4

1 回答 1

2

当我从方法外部访问 NSString “类”时,我会遇到各种古怪的错误。所以我所做的是添加一点保留语句:

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

现在它就像一个魅力。

于 2012-07-21T19:26:40.183 回答