码迷,mamicode.com
首页 > 编程语言 > 详细

Swift学习 (一)

时间:2016-01-09 22:57:41      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

以后会自己总结学习Swift的笔记与深化。希望能够帮助已经有Objective-C经验的开发者更快地学习Swift。我们一起学习,同时也品味到Swift的精妙之处。

 

结论放在开头:我认为Swift比Objective-C更优雅,更安全同时也更现代,更性感。

 

首先学习从Objective-C到Swift的语法差异。我们熟悉的Objective-C特性在Swift中如何展现:

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

Objective-C property in Swift world

在Cocoa世界开发的过程中,我们最常打交道的是property.

典型的声明为:

1
@property (strong,nonatomic) NSString *string;

而在Swift当中,摆脱了C的包袱后,变得更为精炼,我们只需直接在类中声明即可

1
2
3
class Shape {
    var name = "shape"
}

注意到这里,我们不再需要@property指令,而在Objective-C中,我们可以指定property的attribute,例如strong,weak,readonly等。

而在Swift的世界中,我们通过其他方式来声明这些property的性质。

需要注意的几点:

  • strong: 在Swift中是默认的

  • weak: 通过weak关键词申明

1
weak var delegate: UITextFieldDelegate?
  • readonly,readwrie 直接通过声明常量let,声明变量var的方式来指明

  • copy 通过@NSCopying指令声明。

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

  • nonatomic,atomic 所有的Swift properties 都是nonatomic。但是我们在线程安全上已经有许多机制,例如NSLock,GCD相关API等。个人推测原因是苹果想把这一个本来就用的很少的特性去掉,线程安全方面交给平时我们用的更多的机制去处理。

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

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

1
2
3
4
@interface SomeClass : NSObject {
  NSString *someString;
}
@property(nonatomic, copy) NSString* otherString;

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

A Swift property does not have a corresponding instance variable, and the backing store for a property is not accessed directly

小结

  • 因此之前使用OC导致的像巧哥指出的开发争议就不再需要争执了,在Swift的世界里,我们只与property打交道。

  • 并且我们在OC中init和dealloc不能使用属性self.property = XXX来进行设置的情况得以解决和统一。

个人觉得这看似小小一点变动使Swift开发变得更加安全以及在代码的风格更为统一与稳定。

Swift property延伸:

在Swift中,property被分为两类:Stored Properties和Computed properties 简单来说,就是stored properties 能够保存值,而computed properties只提供getter与setter,利用stored properties来生成自己的值。个人感觉Computed properties更像方法,而不是传统意义的属性。但是这样一个特性存在,使得我们更容易组织我们的代码。

Swift提供了语言级别定义类变量的方法。

In C and Objective-C, you define static constants and variables associated with a type as global static variables.In Swift, however, type properties are written as part of the type’s definition, within the type’s outer curly braces, and each type property is explicitly scoped to the type it supports.

在Objective-C中,我们只能通过单例,或者static变量加类方法来自己构造类变量:

1
2
3
4
5
6
7
8
9
10
11
12
@interface Model
+ (int) value;
+ (void) setValue:(int)val;
@end
 
@implementation Model
static int value;
+ (int) value
{ @synchronized(self) { return value; } }
+ (void) setValue:(int)val
{ @synchronized(self) { value = val; } }
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Foo.h
@interface Foo {
}
 
+(NSDictionary*) dictionary;
 
// Foo.m
+(NSDictionary*) dictionary
{
  static NSDictionary* fooDict = nil;
   
  static dispatch_once_t oncePredicate;
   
  dispatch_once(&oncePredicate, ^{
        // create dict
    });
     
  return fooDict;
}

而在Swift中我们通过清晰的语法便能定义类变量:

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

1
2
3
4
5
6
7
8
9
struct SomeStructure {
    static var storedTypeProperty = "Some value."
    static var computedTypeProperty: Int {
        return 1
    }
    class var overrideableComputedTypeProperty: Int {
        return 107
    }
}

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

1
2
3
4
class singletonClass {
    static let sharedInstance = singletonClass()
    private init() {} // 这就阻止其他对象使用这个类的默认的‘()‘初始化方法
}
  • 延伸:目前Swift支持的type propertis中的Stored Properties类型不是传统意义上的类变量(class variable),暂时不能通过class 关键词定义,通过static定义的类变量类似java中的类变量,是无法被继承的,父类与子类的类变量指向的都是同一个静态变量。

1
2
3
4
5
6
class SomeStructure {
    class var storedTypeProperty = "Some value."
}
 
//Swift 2.0 
Error: Class stored properties not yet supported in classes

通过编译器抛出的错误信息,相信在未来的版本中会完善Type properties。

Swift学习 (一)

标签:

原文地址:http://www.cnblogs.com/pengsi/p/5117356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!