Swift学习(一)

1、属性(property)和实例变量(instance variable)

在OC开发中,

@property (strong,nonatomic) NSString *string;

在swift开发中,

class Shape {

  var name = "shape"

}

// strong :在Swift中是默认的。

// weak :关键字weak声明 weak var name:String

// readonley, readwrite 直接通过声明常量let,声明变量var的方式来指明。

// copy :用@NSCopying指令声明。

值得注意的是String,Array和Dictionary在Swift是以值类型(value type)而不是引用类型(reference type)出现,因此它们在赋值,初始化,参数传递中都是以拷贝的方式进行(简单来说,String,Array,Dictionary在Swift中是通过struct实现的)

// Value type example

struct S { var data: Int = -1 }
var a = S()
var b = a						// a is copied to b
a.data = 42						// Changes a, not b
println("\(a.data), \(b.data)")	// prints "42, -1"

// Reference type example

class C { var data: Int = -1 }
var x = C()
var y = x						// x is copied to y
x.data = 42						// changes the instance referred to by x (and y)
println("\(x.data), \(y.data)")	// prints "42, 42"

相关阅读:https://developer.apple.com/swift/blog/?id=10

// nonatomic, atomic 所有的Swift properties 都是nonatomic。在线程安全上已经有许多机制,例如NSLock,GCD相关API等。

-------------------------------------------------------------------------------------------------------------------------------

然后值得注意的是,在Objective-C中,我们可以跨过property直接与instance variable打交道,而在Swift是不可以的。

例如:我们可以不需要将someString声明为property,直接使用即可。即使我们将otherString声明为property,我们也可以直接用_otherString来使用property背后的实例变量。

@interface someClass : NSObject {

  NSString *someStr;

}

@prperty (nonatomic, copy) NSString *otherString;

@end

而在Swift中,我们不能直接与instance variable打交道。也就是我们声明的方式简化为简单的一种,简单来说在Swift中,我们只与property打交道。

在OC中initdealloc不能使用属性self.property = XXX来进行设置的情况得以解决和统一。

关于在init不能直接用self.property = value 阅读iOS内存管理:https://github.com/100mango/zen/blob/master/iOS%E5%A4%AF%E5%AE%9E%EF%BC%9A%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/iOS%E5%A4%AF%E5%AE%9E%EF%BC%9A%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86.md 

-------------------------------------------------------------------------------------------------------------------------------

Swift 的property 分为 Stored Properties 和 Computed properties

stored properties :能够保存值;

computed properties :只提供getter/setter方法,利用stored properties 来生成自己的值。

Switf 单例模式

通过static定义的类变量无法在子类重写,通过class定义的类变量则可在子类重写。

struct SomeStructure {
    static var storedTypeProperty = "Some value."
    static var computedTypeProperty: Int {
        return 1
    }
    class var overrideableComputedTypeProperty: Int {
        return 107
    }
}

同时利用类变量我们也有了更优雅的单例模式实现:

class singletonClass {
    static let sharedInstance = singletonClass()
    private init() {} // 这就阻止其他对象使用这个类的默认的'()'初始化方法
}

 Swift单例阅读扩展:http://krakendev.io/blog/the-right-way-to-write-a-singleton?utm_campaign=This%2BWeek%2Bin%2BSwift&utm_medium=web&utm_source=This_Week_in_Swift_45

 

目前Swift支持的type propertis中的Stored Properties类型不是传统意义上的类变量(class variable),暂时不能通过class 关键词定义,通过static定义的类变量类似java中的类变量,是无法被继承的,父类与子类的类变量指向的都是同一个静态变量。

相关资料: http://stackoverflow.com/questions/24015207/class-variables-not-yet-supported

class SomeStructure {
    class var storedTypeProperty = "Some value."
}

//Swift 2.0 
Error: Class stored properties not yet supported in classes


posted @ 2015-12-09 14:48  J了个M  阅读(283)  评论(0)    收藏  举报