所以我有班级凯蒂:
ref 5
我有一组这些自定义对象# let a = Array.make 3 (Array.make 3 (ref 5));;
val a : int ref array array =
[|[|{contents = 5}; {contents = 5}; {contents = 5}|];
[|{contents = 5}; {contents = 5}; {contents = 5}|];
[|{contents = 5}; {contents = 5}; {contents = 5}|]|]
# a.(1).(2) := 99;;
- : unit = ()
# a;;
- : int ref array array =
[|[|{contents = 99}; {contents = 99}; {contents = 99}|];
[|{contents = 99}; {contents = 99}; {contents = 99}|];
[|{contents = 99}; {contents = 99}; {contents = 99}|]|]
。
我需要使用字符串class Caddy{
var caddyName: String
var phoneNumber: Int
var email: String
var password: String
var caddyRank: String
var caddyLocation: Int
var proPic: UIImage
}
引用数组中的特定对象。
然后我需要更改该特定对象的值,特别是caddyList
值。
如何使用Swift 2.0完成此操作?
答案 0 :(得分:0)
如果订单对您不重要,您应该使用Dictionary Caddys代替一系列Caddys。这将允许O(1)访问集合中的任何类别。使用swift也可以轻松访问和修改对象。
// Assume you have two Caddy objects, caddy1 and caddy2
// Create the dictionary
caddies = [caddy1.caddyName: caddy1, caddy2.caddyName: caddy2] // eg, etc...
// Access and change the caddies location (if the name has a match)
caddies["Bob"]?.caddyLocation = 2
如果您想知道球童实际存在的时间并进行了修改,请将其包裹在if let
中。
let bobCaddyName = "Bob"
if let _ = caddies[bobCaddyName] {
caddies[bobCaddyName]?.caddyLocation = 2
}