我是 swift 新手,我无法获取数组的值,我的数组是这样的
(
"http://ivs.upetch.com/tpaf/storage/uploads/banner/15762295031269.jpg",
"http://ivs.upetch.com/tpaf/storage/uploads/banner/15762294973128.jpg",
"http://ivs.upetch.com/tpaf/storage/uploads/banner/15762294928909.jpg"
)
我的代码是这样的
self.bannerarr = bannerdata as! NSArray
print(self.bannerarr)
for bannerurl in self.bannerarr{
let stringbanner = self.bannerarr .object(at: bannerurl as! Int)
print(stringbanner)
}
但是当我尝试从数组中获取值时,它显示错误为
Could not cast value of type '__NSCFString' (0x1084207a0) to 'NSNumber' (0x10493ed40).
有人可以告诉我我做错了什么吗
最佳答案
如果您想要每个元素的索引及其值,可以使用 enumerated()
迭代数组的方法。
It returns a sequence of pairs (index, element), where index represents a consecutive integer starting at zero and element represents an element of the sequence.
let bannerarr = ["http://ivs.upetch.com/tpaf/storage/uploads/banner/15762295031269.jpg",
"http://ivs.upetch.com/tpaf/storage/uploads/banner/15762294973128.jpg",
"http://ivs.upetch.com/tpaf/storage/uploads/banner/15762294928909.jpg"]
// use your iteration as like
for (index, element) in bannerarr.enumerated() {
print("get Index \(index): getString \(element)")
}
关于ios - 如何在 swift 中使用 NSNumber 和 Int 进行 for in 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59434164/