I'm establishing arrays like this....
我正在建立像这样的数组....
var vidRankArray = [String]()
var vidIdArray = [String]()
var vidTitleArray = [String]()
var vidDescriptionArray = [String]()
var vidImageArray = [String]()
returning Parse info and inserting info like this......
返回解析信息并插入这样的信息......
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)
}
}
I'm trying to insert each element from each vid in their respective arrays in order. I've got if item != nil statements so that if there isn't a particular item it can be skipped over. I'm having trouble doing this. It either throws an array out of index error or index out of range. Confusing because it worked perfect when all my items where appended. Please give me any ideas.
我正在尝试按顺序从每个vid中插入每个元素。我有if item!= nil语句,这样如果没有特定的项目就可以跳过它。我在做这件事时遇到了麻烦。它会抛出索引错误或索引超出范围的数组。令人困惑,因为当我的所有项目都附加时,它工作得很好。请给我任何想法。
****** UPDATE ******* I added this...
******更新*******我添加了这个......
for items in vid!
{
for var i=0; i<vid!.count; ++i
{
if items["vid1"] != nil
and now my console prints out this....
现在我的控制台打印出来....
["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"]
Feels like I'm close. Any Ideas why it's giving me 2 of each out of order?
感觉就像我很亲密。任何想法为什么它给我每个故障中的2个?
******* UPDATE ********
*******更新********
I got it to work sort of. I deleted the var i = 0 statements and made the insertAtIndex's explicit. It worked fine when I definitely had vid1 and vid2. I did vid4 and it fell out of range because it didn't see vid3 or index 2. How can I get it to skip and simply add information for the ones saved i.e. what I was trying to accomplish using var i=0; i
我得到了它的工作。我删除了var i = 0语句并使insertAtIndex显式化。当我肯定有vid1和vid2时它工作正常。我做了vid4并且它超出了范围,因为它没有看到vid3或索引2.我怎样才能跳过它并简单地为保存的信息添加信息,即我试图用var i = 0完成的事情;一世
1 个解决方案
#1
0
Just remove var i
, every insert(variable, atIndex: index)
and replace it with append(variable)
. You have really messy code with integers, sometimes you increment i, sometimes you don't. With append()
, you are sure it will append your item to the array so it will be last and the error with range won't show up.
只需删除var i,每个insert(变量,atIndex:index)并将其替换为append(variable)。你的整数代码实际上很乱,有时你会增加我,有时你却没有。使用append(),您确定它会将您的项目附加到数组,因此它将是最后一个,并且不会显示带范围的错误。
#1
0
Just remove var i
, every insert(variable, atIndex: index)
and replace it with append(variable)
. You have really messy code with integers, sometimes you increment i, sometimes you don't. With append()
, you are sure it will append your item to the array so it will be last and the error with range won't show up.
只需删除var i,每个insert(变量,atIndex:index)并将其替换为append(variable)。你的整数代码实际上很乱,有时你会增加我,有时你却没有。使用append(),您确定它会将您的项目附加到数组,因此它将是最后一个,并且不会显示带范围的错误。