如何在Swift中迭代NSString?

时间:2015-01-04 03:27:12

标签: ios loops swift nsstring nsarray

    var defaults = NSUserDefaults.standardUserDefaults();
    var titlesForTip : [NSString]? = defaults.objectForKey("titleForTipSegment") as? [NSString]
    if titlesForTip == nil     //Check for first run of app
    {
        titlesForTip = ["18%", "20%", "22%"]; //Default value
    }
    // it is ok to get first and last elements
    println( titlesForTip?.first) 

    for item in titlesForTip {//**error [NSString]? does not have a member named Generator**
        println(item)
    }
    //**error type  [NSString]? does not conform to protocol sequence type**
    for (var index, var value) in enumerate(titlesForTip) {
        println("Item \(index + 1): \(value)")
    }

    titlesForTip[1];//**error [NSString]? does not have a member named subscript**

在调试窗口中,它显示titlesForTip,包括3个元素,并显示在titlesForTip [0] ... titlesForTip [2]中,但我不能用[]访问它。

它在Xcode 6.1.1中运行,我不知道如何迭代它。

我的代码出错:                 var titlesForTip:[NSString]? = defaults.objectForKey(“titleForTipSegment”)为? [的NSString]         if(titlesForTip?== nil || titlesForTip!== [])//检查首次运行app         {             titlesForTip = [“18%”,“20%”,“22%”] //默认值         } 当没有任何东西时,objectForKey将返回null数组,因此titlesForTip == nil将永远不会发生。

2 个答案:

答案 0 :(得分:1)

您已创建titlesForTip作为可选项,可能包含NSStrings数组。因此,您需要先检查它是否包含任何内容,然后才能访问它的元素,或者迭代它。

使用与此类似的代码:

var defaults = NSUserDefaults.standardUserDefaults()
var titlesForTip : [NSString]? = defaults.objectForKey("titleForTipSegment") as? [NSString]
if titlesForTip == nil     //Check for first run of app
{
    titlesForTip = ["18%", "20%", "22%"] //Default value
}

if let unwrappedTitlesForTip = titlesForTip {
    // it is ok to get first and last elements
    println( unwrappedTitlesForTip.first) 

    for item in unwrappedTitlesForTip {
        println(item)
    }

    for (var index, var value) in enumerate(unwrappedTitlesForTip) {
        println("Item \(index + 1): \(value)")
    }

    unwrappedTitlesForTip[1] // need to do something with this, won't work just on a line on its own
}

或者,您可以设置它,以便不使用可选项,首先设置默认值,如果从NSDefaults获取内容,则覆盖它,如下所示:

var defaults = NSUserDefaults.standardUserDefaults()
var titlesForTip : [NSString] = ["18%", "20%", "22%"]
if let storedValue = defaults.objectForKey("titleForTipSegment") as? [NSString] {
    titlesForTip = storedValue
}

// it is ok to get first and last elements
println( titlesForTip.first) 

for item in titlesForTip {
    println(item)
}

for (var index, var value) in enumerate(titlesForTip) {
    println("Item \(index + 1): \(value)")
}

titlesForTip[1] // need to do something with this, won't work just on a line on its own

答案 1 :(得分:0)

正如您所知,这仅仅是因为您的数组包含在Optional中 - 因为使用as?运算符将其包装在Optional中。这没关系,这是一个明智的想法,因为它是安全的。但是你必须解开它:

var titlesForTip : [NSString]? // "wrap me in an Optional"
titlesForTip = ["18%", "20%", "22%"]
for item in titlesForTip! { // "unwrap me"
    println(item)
}

在您的实际代码中,我要做的是使用条件绑定来解包:

var titlesForTip : [NSString]? = 
    defaults.objectForKey("titleForTipSegment") as? [NSString]
if let titlesForTip = titlesForTip { "unwrap me safely"
    // ...here, titlesForTip is unwrapped and safe to use...
}

但是,实际上不需要任何代码,因为不需要检查nil并提供默认值。相反,请致电registerDefaults: - 这就是它的用途。这样,objectForKey("titleForTipSegment") 总是具有[NSString]值,您可以无条件投射。