NSDictionary 0键/值对

时间:2022-06-27 18:16:55

Believe me or not, it's the real case. I'm creating a simple NSDictionary in my app and it was working fine. Today I installed Xcode 6 beta and try to run my app with iOS 8. NSDictionary returns 0 key/value pairs while I can see same dictionary works fine in iOS 6 and in iOS 7. Here is the code of that dictionary,

信不信由你,这是事实。我在我的应用中创建了一个简单的NSDictionary它运行得很好。今天我安装了Xcode 6测试版,并尝试使用ios8运行我的应用程序。NSDictionary返回0键/值对,而我可以看到相同的字典在ios6和ios7中运行良好。这是那个字典的代码,

NSDictionary *croppedPhoto = [[NSDictionary alloc] initWithObjectsAndKeys:myImageView.image,@"original",imageView.image,@"cropped", nil];  

NOTE: myImageView and imageView both are UIImageView and I can see the image there in debug mode.
I don't know if something changed in iOS 8 regarding NSDictionary. Appreciate any kind of help in advance. Thanks.

注意:myImageView和imageView都是UIImageView,我可以在调试模式下看到图像。我不知道ios8中关于NSDictionary有什么变化。提前感谢任何帮助。谢谢。

2 个解决方案

#1


4  

Change to this code and see what happens...

更改此代码,看看会发生什么……

NSDictionary *croppedPhoto = @{
    @"original" : myImageView.image,
    @"cropped"  : imageView.image
};

You should be using this format anyway. It has been around for 2 years now.

无论如何,您应该使用这种格式。它已经存在两年了。

Also, it includes run time validation that all the objects are not nil.

此外,它还包括运行时验证,所有对象都不是nil。

If myImageView.image is nil then your code will return a dictionary with no keys or objects, this modern syntax will crash showing you that it is nil.

如果myImageView。图像是nil代码会返回一个没有键或对象的字典,这个现代语法会崩溃显示它是nil。

#2


1  

Does it work if you use modern syntax?

如果你使用现代语法,它还能工作吗?

NSDictionary *croppedPhoto = @{
    @"original": myImageView.image,
    @"cropped":  imageView.image
}

(Which is, admit it, much more readable).

(也就是说,承认吧,它的可读性要高得多)。

#1


4  

Change to this code and see what happens...

更改此代码,看看会发生什么……

NSDictionary *croppedPhoto = @{
    @"original" : myImageView.image,
    @"cropped"  : imageView.image
};

You should be using this format anyway. It has been around for 2 years now.

无论如何,您应该使用这种格式。它已经存在两年了。

Also, it includes run time validation that all the objects are not nil.

此外,它还包括运行时验证,所有对象都不是nil。

If myImageView.image is nil then your code will return a dictionary with no keys or objects, this modern syntax will crash showing you that it is nil.

如果myImageView。图像是nil代码会返回一个没有键或对象的字典,这个现代语法会崩溃显示它是nil。

#2


1  

Does it work if you use modern syntax?

如果你使用现代语法,它还能工作吗?

NSDictionary *croppedPhoto = @{
    @"original": myImageView.image,
    @"cropped":  imageView.image
}

(Which is, admit it, much more readable).

(也就是说,承认吧,它的可读性要高得多)。