迅速array.removeAtIndex错误 [英] swift array.removeAtIndex error
问题描述
为什么我得到错误的NSArray没有一个成员名为removeAtIndex'。我怎样才能解决这个问题?该错误是在第四最后一行。如果我的问题是愚蠢的。对不起,我是相当新编程。我AP preciate所有帮助我得到的。
进口基金会
让userDefaults = NSUserDefaults.standardUserDefaults()FUNC isAppAlreadyLaunchedOnce() - GT;布尔{
让默认值= NSUserDefaults.standardUserDefaults() 如果让isAppAlreadyLaunchedOnce = defaults.stringForKey(isAppAlreadyLaunchedOnce){
的println(应用程序已经启动)
返回true
}
其他{
defaults.setBool(真,forKey:isAppAlreadyLaunchedOnce)
的println(应用推出的第一时间)
返回false
}
}结构newFactBook {
让factsArray = [
蚂蚁舒展当他们在早晨醒来。
鸵鸟跑得比马快。
奥运金牌实际上是做多是银。
你天生有300块骨头,你的时间是一个成年人,你将有206
这需要大约8分钟,光从太阳到达地球。
一些竹类植物可以生长在短短一天差不多一米。
佛罗里达的状态比英格兰大。
有些企鹅可以跨越2-3米露出水面。
平均来说,在坚持66天,形成一个新的习惯。
猛犸仍走在地上时,大金字塔被建造。]} VAR checkLaunch = isAppAlreadyLaunchedOnce()
VAR oldFunFactsArray = [] 如果(checkLaunch == FALSE){
oldFunFactsArray = newFactBook()。factsArray
} 否则,如果(checkLaunch ==真){
oldFunFactsArray = userDefaults.objectForKey(键)作为! NSArray的
}
FUNC randomFacts1() - > (字符串,整数){
VAR unsignedArrayCount = UInt32的(oldFunFactsArray.count)
VAR unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
VAR randomNumber = INT(unsignedRandomNumber)
回报(oldFunFactsArray [randomNumber]作为!字符串,randomNumber) }
oldFunFactsArray.removeAtIndex [randomFacts1()。1] //这里的错误userDefaults.setObject(oldFunFactsArray,forKey:钥匙)
userDefaults.synchronize()
的println(oldFunFactsArray)
我们这里有一些问题:
1
如何调用一个方法 removeAtIndex
是一个接受内部
作为参数的方法。它的不能通过这种方式调用
removeAtIndex [randomFacts1()。1]
相反,你应该写
removeAtIndex(randomFacts1()。1)
2。该类型的 oldFunFactsArray
NSArray的是,这是错误的。
完整的,当你写这样的:
VAR oldFunFactsArray = []
斯威夫特做此推断:
VAR oldFunFactsArray:NSArray的= []
但在这一点上,你有一个一成不变 的NSArray
所以它不具备 removeAtIndex
方法。
由于您使用的斯威夫特,我建议你申报 VAR oldFunFactsArray
如下:
VAR oldFunFactsArray:[字符串]如果checkLaunch == {假
oldFunFactsArray = newFactBook()。factsArray
}其他{
oldFunFactsArray = userDefaults.objectForKey(键)作为! [串]
}
请注意,我在这里声明斯威夫特字符串(S)的阵列。由于我使用 VAR
关键字这个数组将可变,我们将能够调用 removeAtIndex
以后。
另外请注意,在else分支我的力量铸造
objectForKey
的结果[字符串]
。因为我明白了,下面,你正在写的oldFunFactsArray
该插槽会被罚款。
块引用>希望这有助于。
How come I'm getting the error "NSArray does not have a member named 'removeAtIndex'. How can I fix this? The error is on the fourth last line. Sorry if my question is stupid, I'm fairly new to programming. I appreciate all the help I get.
import Foundation let userDefaults = NSUserDefaults.standardUserDefaults() func isAppAlreadyLaunchedOnce()->Bool{ let defaults = NSUserDefaults.standardUserDefaults() if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){ println("App already launched") return true } else{ defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce") println("App launched first time") return false } } struct newFactBook { let factsArray = [ "Ants stretch when they wake up in the morning.", "Ostriches can run faster than horses.", "Olympic gold medals are actually made mostly of silver.", "You are born with 300 bones; by the time you are an adult you will have 206.", "It takes about 8 minutes for light from the Sun to reach Earth.", "Some bamboo plants can grow almost a meter in just one day.", "The state of Florida is bigger than England.", "Some penguins can leap 2-3 meters out of the water.", "On average, it takes 66 days to form a new habit.", "Mammoths still walked the earth when the Great Pyramid was being built."] } var checkLaunch = isAppAlreadyLaunchedOnce() var oldFunFactsArray = [] if(checkLaunch == false){ oldFunFactsArray = newFactBook().factsArray } else if (checkLaunch == true){ oldFunFactsArray = userDefaults.objectForKey("key") as! NSArray } func randomFacts1() -> (String, Int){ var unsignedArrayCount = UInt32(oldFunFactsArray.count) var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount) var randomNumber = Int(unsignedRandomNumber) return (oldFunFactsArray[randomNumber] as! String, randomNumber) } oldFunFactsArray.removeAtIndex[randomFacts1().1] //error here userDefaults.setObject(oldFunFactsArray, forKey:"key") userDefaults.synchronize() println(oldFunFactsArray)
解决方案We have some problems here:
1 How to invoke a method
removeAtIndex
is a method that accepts anInt
as parameters. It cannot be invoked this wayremoveAtIndex[randomFacts1().1]
instead you should write
removeAtIndex(randomFacts1().1)
2. The type of
oldFunFactsArray
is NSArray and it's wrong.Intact when you write this:
var oldFunFactsArray = []
Swift does infer this:
var oldFunFactsArray : NSArray = []
But at this point you have an immutable
NSArray
so it does not have theremoveAtIndex
method.Since you are using Swift I suggest you to declare the
var oldFunFactsArray
as follow:var oldFunFactsArray : [String] if checkLaunch == false { oldFunFactsArray = newFactBook().factsArray } else { oldFunFactsArray = userDefaults.objectForKey("key") as! [String] }
Please note that here I am declaring a Swift array of String(s). Since I use the
var
keyword this array will be mutable and we will be able to invokeremoveAtIndex
later on.Also note that in the else branch I am force-casting the result of
objectForKey
to[String]
. It will be fine since I see, below, you are writing theoldFunFactsArray
in that slot.Hope this helps.
这篇关于迅速array.removeAtIndex错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!