我对 Objective C 和 Cocoa 相当陌生,但是我花了很多时间使用 C++,以前从未遇到过这个问题。
我的应用程序需要按名称识别已安装的磁盘。这是代码:
//this code will run whenever a new disk is mounted to the computer
-(void) scanForKindle:(NSNotification *) notification
{
NSMutableArray *mountedDisks = [[NSMutableArray alloc] init];
mountedDisks = [workspace mountedRemovableMedia];
NSMutableArray *ebookDevices = [[NSMutableArray alloc] init];
NSDictionary *fileAttributes = [[NSDictionary alloc] init];
int currentDisk;
for (currentDisk = 0; currentDisk < [mountedDisks count]; currentDisk++)
{
NSLog(@"Name: %@", [mountedDisks objectAtIndex:currentDisk]);
if ([mountedDisks objectAtIndex:currentDisk] == @"/Volumes/Kindle")
{
NSLog(@"Kindle has been identified");
}
}
}
我已经让 for 循环中的 if 语句一切正常。它根本不会运行。有什么想法为什么吗?我确信这是一个简单的修复,但我一生都无法解决这个问题。
任何帮助将不胜感激!
最佳答案
那是因为您正在 NSString
的两个不同实例之间进行指针比较。
这样做 -
if ([[mountedDisks objectAtIndex:currentDisk] isEqualToString:@"/Volumes/Kindle"])
{
NSLog(@"Kindle has been identified");
}
关于objective-c - 如果语句未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6645474/