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

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.

主站蜘蛛池模板: 成安县| 息烽县| 长岭县| 承德县| 乌兰察布市| 玉环县| 集安市| 德安县| 西乌珠穆沁旗| 稷山县| 甘泉县| 灌南县| 庆阳市| 昌图县| 莆田市| 和平区| 宁陵县| 长沙市| 来凤县| 静海县| 喀喇沁旗| 晋宁县| 南川市| 清水河县| 五指山市| 博乐市| 庄河市| 突泉县| 康平县| 如东县| 嘉善县| 平顶山市| 林芝县| 关岭| 南通市| 桓台县| 新化县| 化隆| 鹿泉市| 三穗县| 工布江达县|