替换 NSMutableArray 的元素
Replace elements of an NSMutableArray
我在 WeatherData 数组中放入了 4 个变量输入。我的阵列似乎也在适当的范围内添加它们(即 [0]-[3] 但我真的想用新信息替换当前信息。
我最初做的是:
if NSUserDefaults.standardUserDefaults().objectForKey("WeatherData") != nil {
WeatherData = NSUserDefaults.standardUserDefaults().objectForKey("WeatherData") as NSMutableArray
DisplayInfo(WeatherData[0] as String, Temperature: WeatherData[1] as Int, Code: WeatherData[2] as String, Condition: WeatherData[3] as String)
}
然后我做一些事情,最后我保存它:
WeatherData.replaceObjectsInRange(0.3, withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
NSUserDefaults.standardUserDefaults().setObject(WeatherData as NSArray, forKey: "WeatherData")
每次迭代我的数组都会变大,就像这样:
(
"San Francisco",
50,
33,
Fair,
"San Francisco",
50,
33,
Fair,
"San Francisco",
50,
33,
Fair,
"San Francisco",
50,
33,
Fair
)
但应该只是
(
"San Francisco",
50,
33,
Fair,
)
意味着内容应该被替换并保存在 NSUserDefaults 中。
在此先感谢您的帮助
王牌
问题是您的错误和 Swift 中的错误(我想)的结合。
你是这样说的:
WeatherData.replaceObjectsInRange(0.3, withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
您正在传递 0.3
(Double
文字),但 replaceObjectsInRange:withObjectsFromArray:
期望该参数为 NSRange
。编译器应该在这里给你一个错误。
但是,编译器会隐式地将您的文字转换为 NSMakeRange(0,0)
,因此 replaceObjectsInRange:withObjectsFromArray:
会在数组的开头插入新元素。
试试这个:
WeatherData.replaceObjectsInRange(NSRange(0 ... 3), withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
感谢 rob mayoff 的帮助,这里是我的代码的副本:
if WeatherData.count == 0 {
WeatherData.addObjectsFromArray([placemark.locality, Temperature, Code, Condition])
} else {
WeatherData.replaceObjectsInRange(NSRange(0 ... 3), withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
}
我在 WeatherData 数组中放入了 4 个变量输入。我的阵列似乎也在适当的范围内添加它们(即 [0]-[3] 但我真的想用新信息替换当前信息。
我最初做的是:
if NSUserDefaults.standardUserDefaults().objectForKey("WeatherData") != nil {
WeatherData = NSUserDefaults.standardUserDefaults().objectForKey("WeatherData") as NSMutableArray
DisplayInfo(WeatherData[0] as String, Temperature: WeatherData[1] as Int, Code: WeatherData[2] as String, Condition: WeatherData[3] as String)
}
然后我做一些事情,最后我保存它:
WeatherData.replaceObjectsInRange(0.3, withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
NSUserDefaults.standardUserDefaults().setObject(WeatherData as NSArray, forKey: "WeatherData")
每次迭代我的数组都会变大,就像这样:
(
"San Francisco",
50,
33,
Fair,
"San Francisco",
50,
33,
Fair,
"San Francisco",
50,
33,
Fair,
"San Francisco",
50,
33,
Fair
)
但应该只是
(
"San Francisco",
50,
33,
Fair,
)
意味着内容应该被替换并保存在 NSUserDefaults 中。
在此先感谢您的帮助 王牌
问题是您的错误和 Swift 中的错误(我想)的结合。
你是这样说的:
WeatherData.replaceObjectsInRange(0.3, withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
您正在传递 0.3
(Double
文字),但 replaceObjectsInRange:withObjectsFromArray:
期望该参数为 NSRange
。编译器应该在这里给你一个错误。
但是,编译器会隐式地将您的文字转换为 NSMakeRange(0,0)
,因此 replaceObjectsInRange:withObjectsFromArray:
会在数组的开头插入新元素。
试试这个:
WeatherData.replaceObjectsInRange(NSRange(0 ... 3), withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
感谢 rob mayoff 的帮助,这里是我的代码的副本:
if WeatherData.count == 0 {
WeatherData.addObjectsFromArray([placemark.locality, Temperature, Code, Condition])
} else {
WeatherData.replaceObjectsInRange(NSRange(0 ... 3), withObjectsFromArray: [placemark.locality, Temperature, Code, Condition])
}