iOS:一种在Swift中定义NSArray内部的常规对象的方法

时间:2023-01-16 16:15:27

In Swift I have this object City:

在Swift我有这个对象城市:

class City {

    var name:String
    init(name: String) {
        self.name = name
    }

    var file:String?
    var description:String?
}

now in my viewcontroller I fill an array with these type of objects and I want to fill a tableview, but I'm not able to access at the property of this object.

现在在我的viewcontroller中我用这些类型的对象填充数组,我想填充tableview,但我无法访问此对象的属性。

I show you the code, that doesn't take the property "file":

我告诉你代码,不带属性“文件”:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = menu_list.dequeueReusableCellWithIdentifier("list_cell") as ListCell
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    var obj = list_element.objectAtIndex(indexPath.row)

    var name = obj.file
    return cell
}

Now STOP! You can suggest me to add this "as City"

现在停止!您可以建议我添加“作为城市”

var obj = list_element.objectAtIndex(indexPath.row) as City

but I don't know what type of objects is filled my array, I have other object as Country and State, then what's the way to have a general managing of objects? In obj-c I did it with "id" but in swift?

但我不知道我的数组填充了什么类型的对象,我有其他对象作为国家和州,那么对象的一般管理方式是什么?在obj-c中我用“id”做了但是快速?

1 个解决方案

#1


0  

In Swift, you can use an NSArray if you like, you just have to make sure all of your objects are subclasses of NSObject. Swift also allows you to declare an array of type [Any] which can hold any type. You then use conditional cast as? to work with the objects:

在Swift中,如果您愿意,可以使用NSArray,您只需要确保所有对象都是NSObject的子类。 Swift还允许您声明一个类型为[Any]的数组,它可以包含任何类型。然后你使用条件转换为?使用对象:

class City {

}

class Country {

}

class State {

}

var places = [Any]()

places.append(City())
places.append(State())
places.append(Country())
places.append(Country())
places.append(13)

for place in places {
    if let loc = place as? City {
        println("processing city")
    }
    if let loc = place as? State {
        println("processing state")
    }
    if let loc = place as? Country {
        println("processing country")
    }
}

You can also use a switch to determine the type:

您还可以使用开关来确定类型:

for place in places {
    switch place {
    case let loc as City:
        println("I see a city")
    case let loc as State:
        println("I see a state")
    case let loc as Country:
        println("I see a country")
    default:
        println("I don't know what it is")
    }
}

If all you are putting in your array of places is City, State, and Country objects, it would be better form to have those three classes derive from a base class (let's call it Location) and then have your array of places be of type [Location].

如果您在所有位置放置的是City,State和Country对象,那么将这三个类派生自基类(让我们称之为Location)然后让您的数组类型为类型将是更好的形式[地点]。

#1


0  

In Swift, you can use an NSArray if you like, you just have to make sure all of your objects are subclasses of NSObject. Swift also allows you to declare an array of type [Any] which can hold any type. You then use conditional cast as? to work with the objects:

在Swift中,如果您愿意,可以使用NSArray,您只需要确保所有对象都是NSObject的子类。 Swift还允许您声明一个类型为[Any]的数组,它可以包含任何类型。然后你使用条件转换为?使用对象:

class City {

}

class Country {

}

class State {

}

var places = [Any]()

places.append(City())
places.append(State())
places.append(Country())
places.append(Country())
places.append(13)

for place in places {
    if let loc = place as? City {
        println("processing city")
    }
    if let loc = place as? State {
        println("processing state")
    }
    if let loc = place as? Country {
        println("processing country")
    }
}

You can also use a switch to determine the type:

您还可以使用开关来确定类型:

for place in places {
    switch place {
    case let loc as City:
        println("I see a city")
    case let loc as State:
        println("I see a state")
    case let loc as Country:
        println("I see a country")
    default:
        println("I don't know what it is")
    }
}

If all you are putting in your array of places is City, State, and Country objects, it would be better form to have those three classes derive from a base class (let's call it Location) and then have your array of places be of type [Location].

如果您在所有位置放置的是City,State和Country对象,那么将这三个类派生自基类(让我们称之为Location)然后让您的数组类型为类型将是更好的形式[地点]。