随着Xcode 8的发布,swift 3.0终于来了。又有一大批api名字发生了变化。但是感觉3.0之后的变化应该会小些,因此再重新仔细学习一下。
1. 关于swift引入的Computed Properties概念。
今天看了Salesforce的sdk,发现了如下的demo代码:
#import "SObjectData.h" @interface SampleRequestSObjectData : SObjectData @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *contactId; @property (nonatomic, copy) NSString *contactName; @property (nonatomic, copy) NSString *productId; @property (nonatomic, copy) NSString *productName; @property (nonatomic, copy) NSString *deliveryDate; @property (nonatomic, copy) NSNumber *quantity; @property (nonatomic, copy) NSString *status; @property (nonatomic, copy) NSDictionary *authorizedUsers; @property (nonatomic, copy) NSArray *userRecords; @property (nonatomic, copy) NSArray *attachments; @end // // SampleRequestSObjectData.m // SFDCOfflinePoc // // Created by PAULO VITOR MAGACHO DA SILVA on 1/24/16. // Copyright © 2016 Topcoder Inc. All rights reserved. // #import "SampleRequestSObjectData.h" #import "SampleRequestSObjectDataSpec.h" #import "SObjectData+Internal.h" #import <SmartSync/SFSmartSyncConstants.h> @implementation SampleRequestSObjectData + (SObjectDataSpec *)dataSpec { static SampleRequestSObjectDataSpec *sDataSpec = nil; if (sDataSpec == nil) { sDataSpec = [[SampleRequestSObjectDataSpec alloc] init]; } return sDataSpec; } #pragma mark - Property getters / setters - (NSString *)name { return [self nonNullFieldValue:kSampleRequestNameField]; } - (void)setName:(NSString *)name { [self updateSoupForFieldName:kSampleRequestNameField fieldValue:name]; } - (NSString *)contactId { return [self nonNullFieldValue:kSampleRequestContactField]; } - (void)setContactId:(NSString *)contactId { [self updateSoupForFieldName:kSampleRequestContactField fieldValue:contactId]; } - (NSString *)contactName { return [[self nonNullFieldValue:kSampleRequestContactQueryField] objectForKey:@"Name"]; } - (NSString *)productId { return [self nonNullFieldValue:kSampleRequestProductField]; } - (void)setProductId:(NSString *)productId { [self updateSoupForFieldName:kSampleRequestProductField fieldValue:productId]; } - (NSString *)productName { return [[self nonNullFieldValue:kSampleRequestProductQueryField] objectForKey:@"Name"]; } - (NSString *)deliveryDate { return [self nonNullFieldValue:kSampleRequestDeliveryDateField]; } - (void)setDeliveryDate:(NSString *)deliveryDate { [self updateSoupForFieldName:kSampleRequestDeliveryDateField fieldValue:deliveryDate]; } - (NSString *)quantity { return [self nonNullFieldValue:kSampleRequestQuantityField]; } - (void)setQuantity:(NSString *)quantity { [self updateSoupForFieldName:kSampleRequestQuantityField fieldValue:quantity]; } - (NSString *)status { return [self nonNullFieldValue:kSampleRequestStatusField]; } - (void)setStatus:(NSString *)status { [self updateSoupForFieldName:kSampleRequestStatusField fieldValue:status]; } - (NSDictionary *)authorizedUsers { return [self nonNullFieldValue:kSampleRequestAuthorizedUsersField]; } - (void)setAuthorizedUsers:(NSDictionary *)authorizedUsers { [self updateSoupForFieldName:kSampleRequestAuthorizedUsersField fieldValue:authorizedUsers]; } - (NSArray *)attachments { return [self nonNullFieldValue:kSampleRequestAttachmentsField]; } - (void)setAttachments:(NSArray *)attachment { [self updateSoupForFieldName:kSampleRequestAttachmentsField fieldValue:attachment]; } - (NSArray *) userRecords { int totalSize = [[self.authorizedUsers objectForKey:@"totalSize"] intValue]; if (totalSize > 0) { return [self.authorizedUsers objectForKey:@"records"]; } return nil; } @end
通过代码,可以看出,声明的几个@property 其实没有对每个property对应的真实变量进行读写操作,而是通过自定义的get,set方法对其他的数据进行操作,但是系统会不会自动建立那些没用的真实变量呢?从代码上是看不出了。而且从声明的地方也看不出这些property的特殊性,不注意还真容易误解。
swift的Computed Properties,解决了这个问题:
struct Rect { var origin = Point() var size = Size() var center: Point { get { let centerX = origin.x + (size.width / 2) let centerY = origin.y + (size.height / 2) return Point(x: centerX, y: centerY) } set(newCenter) { origin.x = newCenter.x - (size.width / 2) origin.y = newCenter.y - (size.height / 2) } } }
这里的center,有自定义的get,set方法,就明确地指出了它的目的不是为了保存一个值,而是“provide a getter and an optional setter to retrieve and set other properties and values indirectly.”
这个写法就比以前的oc写法要好上很多啊!
时间: 2024-10-12 12:00:53