官术网_书友最值得收藏!

Modern syntax

Swift has a modern syntax that eliminates the verbosity of programming languages such as Objective-C. For instance, the following code example shows an Objective-C class with a property and method. Objective-C classes are defined in two separate files (interface and implementation). The VerboseClass.h file defines an interface as a subclass of the NSObject class. It defines a property, ourArray, and a method, aMethod.

The implementation file imports the header class and provides an implementation for aMethod, as shown in the following code:

//  VerboseClass.h 
@interface VerboseClass: NSObject
@property (nonatomic, strong) NSMutableArray *ourArray;
- (void)aMethod:(NSMutableArray*)anArray;
@end

// VerboseClass.m
#import "VerboseClass.h"

@implementation VerboseClass
- (void)aMethod:(NSMutableArray*)anArray {
self.ourArray = [[NSMutableArrayalloc] initWithArray: @[@"One",
@"Two", @"Three"]];
}

A similar functionality in Swift can be achieved as follows:

class ASwiftClass { 
var ourArray: [String] = []

func aMethod(anArray: [String]) {
self.ourArray = anArray
}
}

let aSwiftClassInstance = ASwiftClass()
aSwiftClassInstance.aMethod(anArray: ["one", "Two", "Three"])
print(aSwiftClassInstance.ourArray)

As seen from this example, Swift eliminates a lot of unnecessary syntax and keeps code very clean and readable.

主站蜘蛛池模板: 石阡县| 通许县| 兴宁市| 临西县| 神池县| 洛川县| 手游| 三门峡市| 融水| 凌海市| 娱乐| 伊春市| 乌拉特前旗| 宜章县| 七台河市| 岑溪市| 株洲县| 宣威市| 渝中区| 陇西县| 凤城市| 永清县| 奇台县| 璧山县| 临城县| 澄城县| 福海县| 绥宁县| 土默特左旗| 深水埗区| 房产| 合川市| 嘉义市| 合水县| 东阳市| 牙克石市| 黄龙县| 都昌县| 阿瓦提县| 澳门| 九龙县|