objective-c - 如果语句未运行

标签 objective-c cocoa

我对 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/

相关文章:

ios - XMPPFramework - 如何将图像从一台设备传输到另一台设备?

iphone - UIPinchGesture 无法在 MPMoviePlayerController 上运行?

xcode - 验证 GateKeeper (Sierra) 的 OSX 应用程序签名时出现问题 ["the code is valid but does not seem to be an app"]

macos - NSFileWrapper 有时返回 nil

objective-c - NSMapTable 和 NSMutableDictionary 的区别

ios - 我可以修改默认 iOS 控件的 subview 吗?

objective-c - 从属性列表中读取 NSDictionary 键

ios - 检查用户在应用内购买中购买的产品

cocoa - 当文件名中有空格时 NSImage 返回 0x0

cocoa - NSTimer - 设置普通 Vanilla - 不触发