NSArray,NSUserDefaults,可选项和解包混乱(NSArray, NSUserDefaults, optionals and unwrapping mess)

编程入门 行业动态 更新时间:2024-10-07 12:21:50
NSArray,NSUserDefaults,可选项和解包混乱(NSArray, NSUserDefaults, optionals and unwrapping mess)

在处理显然简单的事情时,我有非常讨厌的错误。

我有一个NSArray ,我在应用程序第一次启动时填写了单词。

然后在某一点我想使用数组,所以我在类中声明了另一个数组:

var localArray = NSUserDefaults.standardUserDefaults().arrayForKey("array")!

然后我想在标签中显示一个单词。 当用户按下按钮(向前/向后)时,出现下一个/前一个单词。 很简单吧? 它是,但不知何故,我得到了非常讨厌的错误。

我有2个方法用于下一个/前一个单词:

@IBAction func nextWord(sender: AnyObject) { if let currentIndex = localArray.indexOf(label.text!) { if currentIndex == localArray.count-1 { label.text = localArray[0] as? String } else { label.text = localArray[currentIndex+1] } } }

倒退的另一种方法是相同的(必要的修改)。

在viewDidLoad我只设置标签的文本:

label.text = localArray[0] as? String

问题出在if let语句中。 在使用用户默认值之前,我只是用几个字符串初始化了localArray 。 一切都很好。 但是一旦我添加了用户默认值,疯狂就开始了。 我到处都有错误,我使用localArray说数组没有打开。 所以我加了! 用户在初始化本地数据时默认数组。

但是最后2个错误(在if let语句中)没有任何意义。 现在indexOf()方法抛出一个错误,这是之前没有发生的事情。 它还告诉我,它无法将字符串(label.text!)转换为@noescape (AnyObject) throws -> Bool 。

所以现在我需要将该字符串转换为NSString以使其工作或者什么? 我发现了一些与用户默认值中的数组相关的“桥接”。 那是我的问题吗?

现在这就是我的错误:为什么我必须将数组中的字符串显式向下转换为字符串? 为什么indexOf方法会发生变化。

一个未打开的可选行为不是非可选的吗?

I have really nasty errors while working with something apparently simple.

I have an NSArray which I fill with words the first time the app launches.

Then at a certain point I want to use the array, so I declared another array inside the class:

var localArray = NSUserDefaults.standardUserDefaults().arrayForKey("array")!

Then I want to display a word in a label. And when a user presses a button (forward/backward) the next/previous word appears. Pretty simple, right? It is, but somehow I get really nasty errors.

I have 2 methods for the next/previous words:

@IBAction func nextWord(sender: AnyObject) { if let currentIndex = localArray.indexOf(label.text!) { if currentIndex == localArray.count-1 { label.text = localArray[0] as? String } else { label.text = localArray[currentIndex+1] } } }

The other method for going backwards is identical (with necessary modifications).

And in viewDidLoad I just set the label's text:

label.text = localArray[0] as? String

The problem is with the if let statements. Before using the user defaults, I just initialized localArray with a few strings. And everything worked fine. But once I added the user defaults, madness began. I had errors everywhere I used localArray saying that the array isn't unwrapped. So I added ! after the user defaults array when initializing the local one.

But the last 2 errors (in the if let statements) make no sense. Now the indexOf() method throws an error, a thing that didn't happen before. And it also tells me that it can't convert the string (label.text!) to @noescape (AnyObject) throws -> Bool.

So now I need to transform that string to an NSString for that to work or what? I found something about "bridging" related to arrays in user defaults. Is that my issue?

Now here's what bugs me: WHY do I have to explicitly downcast a string in the array to a string? WHY did the indexOf method change.

Doesn't an unwrapped optional act like a non-optional?

最满意答案

您正在创建的对象不是数组(至少编译器不知道它是什么)。 函数arrayForKey("array")! 返回AnyObject ,而不是Array 。 所以AnyObject编译器认为这个对象是一个AnyObject 。 我如何解决这个问题(可能不是正确的方法)是立即强制转换数组:

if let myArray: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("array") { //Your array exists, cast it now and use it var localArray = myArray as! Array<String> } else { //Something bad happened, the array isn't there. }

然后你将不得不在以后解包。 如果您知道此对象将始终存在并且演员阵容将始终有效,您可以更改? 来! 。

请查看此答案以获取更多信息。

The object you are creating is not an array (at least the compiler doesn't know it is). The function arrayForKey("array")! is returning an AnyObject, not an Array. So from there on out the compiler thinks this object is an AnyObject. How I would solve this (may not be the correct way) would be to cast the Array immediately:

if let myArray: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("array") { //Your array exists, cast it now and use it var localArray = myArray as! Array<String> } else { //Something bad happened, the array isn't there. }

Then you will have to do unwrapping later on. If you know this object will always exist and the cast will always work you can change the ? to !.

Look to this answer for a little more information.

更多推荐

本文发布于:2023-07-27 04:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   混乱   NSUserDefaults   NSArray   unwrapping

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!