如何在多个数组的索引处保存多个元素?
Posted
技术标签:
【中文标题】如何在多个数组的索引处保存多个元素?【英文标题】:How to save multiple elements at index for multiple arrays? 【发布时间】:2015-11-17 21:38:44 【问题描述】:我正在建立这样的数组......
var vidRankArray = [String]()
var vidIdArray = [String]()
var vidTitleArray = [String]()
var vidDescriptionArray = [String]()
var vidImageArray = [String]()
返回解析信息并插入这样的信息......
override func viewWillAppear(animated: Bool)
let query = PFQuery(className: "UserVideos")
query.whereKey("userObjectId", equalTo: PFUser.currentUser()!.objectId!)
query.findObjectsInBackgroundWithBlock (vid:Array?, error:NSError?) -> Void in
if vid != nil
for items in vid!
var i:Int
i = 0
if items["vid1"] != nil
if let myfav1 = items["vid1"] as? NSArray
let id = myfav1[0] as! String
let rank = myfav1[1] as! String
let title = myfav1[2] as! String
let description = myfav1[3] as! String
let image = myfav1[4] as! String
self.vidRankArray.insert(rank, atIndex: i)
self.vidIdArray.insert(id, atIndex: i)
self.vidTitleArray.insert(title, atIndex: i)
self.vidDescriptionArray.insert(description, atIndex: i)
self.vidImageArray.insert(image, atIndex: i)
if items["vid2"] != nil
if let myfav2 = items["vid2"] as? NSArray
let id = myfav2[0] as! String
let rank = myfav2[1] as! String
let title = myfav2[2] as! String
let description = myfav2[3] as! String
let image = myfav2[4] as! String
self.vidRankArray.insert(rank, atIndex: ++i)
self.vidIdArray.insert(id, atIndex: ++i)
self.vidTitleArray.insert(title, atIndex: ++i)
self.vidDescriptionArray.insert(description, atIndex: ++i)
self.vidImageArray.insert(image, atIndex: ++i)
if items["vid3"] != nil
if let myfav3 = items["vid3"] as? NSArray
let id = myfav3[0] as! String
let rank = myfav3[1] as! String
let title = myfav3[2] as! String
let description = myfav3[3] as! String
let image = myfav3[4] as! String
self.vidRankArray.insert(rank, atIndex: ++i)
self.vidIdArray.insert(id, atIndex: ++i)
self.vidTitleArray.insert(title, atIndex: ++i)
self.vidDescriptionArray.insert(description, atIndex: ++i)
self.vidImageArray.insert(image, atIndex: ++i)
我正在尝试将每个 vid 中的每个元素按顺序插入各自的数组中。我有 if item != nil 语句,因此如果没有特定项目,则可以跳过它。我很难做到这一点。它要么抛出一个数组超出索引错误或索引超出范围。令人困惑,因为当我的所有物品都附加时它工作得很好。请给我任何想法。
****** 更新 ******* 我添加了这个...
for items in vid!
for var i=0; i<vid!.count; ++i
if items["vid1"] != nil
现在我的控制台打印出这个......
["2", "2", "1", "1"]
["3fy4cqWMhyI", "3fy4cqWMhyI", "TI0DGvqKZTI", "TI0DGvqKZTI"]
["Wonder Girls (원더걸스) - Be My Baby", "Wonder Girls (원더걸스) - Be My Baby", "EXO-K_중독(Overdose)_Music Video", "EXO-K_중독(Overdose)_Music Video"]
["Wonder Girls\' 2nd Album Title Song \"Be My Baby\" Copyrightⓒ 2011. JYP Entertainment. All Rights Reserved ▣ Wonder Girls 2nd Album Site ...", "Wonder Girls\' 2nd Album Title Song \"Be My Baby\" Copyrightⓒ 2011. JYP Entertainment. All Rights Reserved ▣ Wonder Girls 2nd Album Site ...", "The music video of EXO\'s \'Overdose\' is released! EXO is back with a new mini album with a more powerful performance that will make you \'overdose\' on their ...", "The music video of EXO\'s \'Overdose\' is released! EXO is back with a new mini album with a more powerful performance that will make you \'overdose\' on their ..."]
["https://i.ytimg.com/vi/3fy4cqWMhyI/default.jpg", "https://i.ytimg.com/vi/3fy4cqWMhyI/default.jpg", "https://i.ytimg.com/vi/TI0DGvqKZTI/default.jpg", "https://i.ytimg.com/vi/TI0DGvqKZTI/default.jpg"]
感觉就像我很接近。任何想法为什么它给我每个乱序的 2 个?
*********更新********
我让它工作了。我删除了 var i = 0 语句并明确了 insertAtIndex。当我肯定有 vid1 和 vid2 时,它工作得很好。我做了 vid4,但它超出了范围,因为它没有看到 vid3 或索引 2。我怎样才能让它跳过并简单地为保存的那些添加信息,即我试图使用 var i=0;一世
【问题讨论】:
考虑使用struct oder自定义类mit属性rank
、id
、title
、description
和image
分别保存一项的属性。
请参阅第二次更新。我有点让它工作。如何跳过每个项目并仅添加存在的元素?
【参考方案1】:
只需删除var i
,每个insert(variable, atIndex: index)
并将其替换为append(variable)
。您的整数代码非常混乱,有时您会增加 i,有时您不会。使用append()
,您确定它会将您的项目附加到数组中,因此它将是最后一个并且不会显示范围错误。
【讨论】:
以上是关于如何在多个数组的索引处保存多个元素?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 javascript/jquery 中使用空元素在第三个索引处创建任何数组,例如 ['Aarav', 10, '', , '']