我正在建立像这样的数组......
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完成的事情;一世
答案 0 :(得分:0)
每次var i
删除insert(variable, atIndex: index)
,然后将其替换为append(variable)
。你有完整的代码乱码,有时你会增加我,有时你不会。使用append()
,您确定它会将您的项目附加到数组中,因此它将是最后一个,并且范围内的错误不会显示。