迅速将来自不同类的数组追加到数组 [英] append to array from different class in swift

查看:100
本文介绍了迅速将来自不同类的数组追加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试附加到我的数组

  var feedArray:[AnyObject] = [] 

来自另一个类。
我有2个类:

  class FeedViewController:UIViewController,UICollectionViewDataSource,
UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

var feedArray:[AnyObject] = []

  class AddEditViewController:UIViewController,NSFetchedResultsControllerDelegate,
UIImagePickerControllerDelegate,UINavigationControllerDelegate {

我想在我的 AddEditViewController中附加数组 feedArray。
但是我不能。
我尝试过:

  FeedViewController()。feedArray.append( TheObjectImTryingToAdd)

我什至试图放

  print(FeedViewController()。feedArray.count)

,即使数组有3个对象。



我在做什么错?如果您想查看详细的代码,这是我以前的问题:为什么在我重新搜索时主视图控制器不会更新?

解决方案

经过一番尝试之后,我找到了解决方法



首先,将数组添加到类之外,使其具有全局性,而不是:

  import UIKit 
import CoreData
import MobileCoreServices

class FeedViewController:UIViewController ,UICollectionViewDataSource,
UICollectionViewDelegate,UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

@IBOutlet弱var collectionView:UICollectionView!
var feedArray:[AnyObject] = []

更改它,以便您拥有:

 导入UIKit 
导入CoreData
导入MobileCoreServices

var feedArray:[AnyObject] = []

类FeedViewController:UIViewController,UICollectionViewDataSource,
UICollectionViewDelegate,UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

@IBOutlet弱var collectionView:UICollectionView!

而不是使用

  FeedViewController()。feedArray.append(feedItem)

现在可以使用:

  feedArray.append(feedItem)

问题出在哪里,是我每次使用: FeedViewController()。fee ...我创建了一个新实例,所以实际上我没有正确地追加数组。 / p>

我希望对您有所帮助。


Im trying to append to my array

var feedArray: [AnyObject] = []

from another class. I have 2 classes:

class FeedViewController: UIViewController, UICollectionViewDataSource,
     UICollectionViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

      var feedArray: [AnyObject] = []

and

class AddEditViewController: UIViewController, NSFetchedResultsControllerDelegate,
      UIImagePickerControllerDelegate, UINavigationControllerDelegate {

I want to in my "AddEditViewController" append the array "feedArray". But I'm not able. I have tried:

FeedViewController().feedArray.append("TheObjectImTryingToAdd")

I even tried to put

print(FeedViewController().feedArray.count)

but the result is always 0 even though the array have 3 objects.

what I'm i doing wrong? if you want to see detailed code, this is my previous question: why won't my main viewcontroller update when i segue back?

解决方案

So, After playing around a little bit and trying to learn, I found the solution

First of all, add the array outside of the class so it is global, so instead of:

import UIKit
import CoreData
import MobileCoreServices

class FeedViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

   @IBOutlet weak var collectionView: UICollectionView!
   var feedArray: [AnyObject] = []

change it so you have:

import UIKit
import CoreData
import MobileCoreServices

 var feedArray: [AnyObject] = []

class FeedViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

  @IBOutlet weak var collectionView: UICollectionView!

And instead of using

FeedViewController().feedArray.append(feedItem)

Now you can use:

feedArray.append(feedItem)

What the problem was, is that every time i used : "FeedViewController().fee..." I created a new instance, so i actually didn't append the array properly.

I hope it helped.

这篇关于迅速将来自不同类的数组追加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆