在Swift中创建多个数组

时间:2021-02-28 12:55:05

i have an object with some proprieties, arrays of classes or structs

我有一个对象,它有一些属性,类或结构的数组

I want to put those proprieties in an array so i can access them easily in TableDataSource ( because of section, indexpath etc.. )

我想把这些属性放到一个数组中,这样我就可以在TableDataSource中方便地访问它们(由于section、indexpath等原因)。

I've tried writing this initialization

我尝试过写这个初始化

var dataStructure = Array<Any>()

    var tweet : Tweet?{
        didSet{
            dataStructure.removeAll()
            dataStructure.append(tweet?.media)
            dataStructure.append(tweet?.urls )
            dataStructure.append(tweet?.hashtags)
            dataStructure.append(tweet?.userMentions)


        }
    }

and then retrieving it

然后检索

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    let arr = dataStructure[section] as? NSArray

    if arr != nil{
        println(arr!)
        return arr!.count
    }else{
        return 0
    }
}

but the arr is always nil

但是arr总是为零

maybe is because i'm casting to NSArray? but i can't cast to "Array"

也许是因为我要选NSArray?但是我不能把它转换成数组

media , urls , hastags, and userMentions are initialized in the "Tweet" class

在“Tweet”类中初始化了媒体、url、hastags和user提及。

public var media = [MediaItem]()
public var hashtags = [IndexedKeyword]()
public var urls = [IndexedKeyword]()
public var userMentions = [IndexedKeyword]()

so they are pretty normal arrays

它们是很普通的数组

1 个解决方案

#1


1  

Basically there is a few problems with your code:

基本上你的代码有几个问题:

1) didSet is not calling during init. This cause dataStructure array to be empty;

1)在init期间didSet没有调用。这导致数据结构数组为空;

2) Are you sure that you need cast to NSArray? You can get count without it.

2)你确定要给NSArray投石膏吗?没有它你也可以计数。

3) It's will be much better to use computed property in Tweet class to receive required data.

3)使用Tweet类中的computed property接收所需数据会更好。

public class Tweet {
    public var media = [MediaItem]()
    public var hashtags = [IndexedKeyword]()
    public var urls = [IndexedKeyword]()
    public var userMentions = [IndexedKeyword]()

    public var dataStructure:[Any] {
        return [media, hashtags, urls, userMentions]
    }
}

#1


1  

Basically there is a few problems with your code:

基本上你的代码有几个问题:

1) didSet is not calling during init. This cause dataStructure array to be empty;

1)在init期间didSet没有调用。这导致数据结构数组为空;

2) Are you sure that you need cast to NSArray? You can get count without it.

2)你确定要给NSArray投石膏吗?没有它你也可以计数。

3) It's will be much better to use computed property in Tweet class to receive required data.

3)使用Tweet类中的computed property接收所需数据会更好。

public class Tweet {
    public var media = [MediaItem]()
    public var hashtags = [IndexedKeyword]()
    public var urls = [IndexedKeyword]()
    public var userMentions = [IndexedKeyword]()

    public var dataStructure:[Any] {
        return [media, hashtags, urls, userMentions]
    }
}