Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Safer Networking Layer With Swift

Safer Networking Layer With Swift

Yusei Nishiyama

July 14, 2015
Tweet

More Decks by Yusei Nishiyama

Other Decks in Technology

Transcript

  1. [CKDPantrymanInitializer setVariables]; [[CKDPushNotificationManager sharedManager] start]; [[CKDPushNotificationManager sharedManager] registerForRemoteNotification]; [[CKDBargainManager sharedManager]

    start]; [[CKDDeviceTrackingManager sharedManager] start]; [[CKDInitializeConfigManager sharedManager] start]; [[CKDSupportContactsManager sharedManager] start]; [[CKDAccountManager sharedManager] migrateStoredAccount]; [[CKDGoogleAnalyticsManager sharedManager] startTracking]; [[CKDHistoryManager sharedManager] load]; [[CKDRecipesManager sharedManager] load]; #FDBVTFPG0CKFDUJWF$ TZOUBY /P JU`TOPU
 4FFUIFHFPNFUSJDCFBVUZ
  2. 1SPT NSArray *array = @[[Recipe new]]; BOOL boolValue = [array[0]

    boolValue]; :PVDBOEPBOZUIJOHZPVXBOUUP 4VQFSC name = "John" name = Time.now() name = 123.45
  3. *O0CKFDUJWF$ NSDictionary *parameters = @{ @"page": @(page), @"per_page": @10, };

    [client requestGetWithPath:path parameters:parameters completion:^(NSArray *recipes, NSError *error) { if (nil == error) { } else { } }]; There are 4 possibilities The type of response is determined dynamically SFDJQFTOJM SFDJQFTOJM FSSPSOJM GBJMVSF FSSPSOJM TVDDFTT
  4. *O4XJGU let params = PaginationParams(order: .Desc(field: .Publish), page: 1, perPage:

    10) let api = APIs.Recipes.Get(paginationParams: params) SharedAPIClient.sendRequest(api) { response in switch response { case .Success(let value): value[0].cookingTime case .Failure(let value): value.message } } The type of the response can be determined statically You can handle success and failure exhaustively
  5. &OVN enum Order : Printable { enum Field: Printable {

    case Update, Publish var description: String { switch self { case .Update: return "updated_at" case .Publish: return "published_at" } } } case Desc(field: Field), Asc(field: Field) var description: String { switch self { case .Desc(let field): return "\(field)_desc" case .Asc(let field): return "\(field)_asc" } } } Enum can conform protocols Enum can have properties and functions You can associate a value with an enum let params = PaginationParams(order: .Desc(field: .Publish), page: 1, perPage: 10)
  6. 1SPUPDPM protocol API { typealias ResponseType class Recipes { class

    Get: API { typealias ResponseType = PagedRecipes Associated Types A type conform the protocol can determine an actual type
  7. /VMMBCJMJUZ  *NNVUBCJMJUZ struct Recipe { var id: Int? var

    title = "" let user: User This can be nil This is immutable This can’t be nil and has default value This can’t be nil
  8. 1VUFWFSZUIJOHUPHFUIFS let params = PaginationParams(order: .Desc(field: .Publish), page: 1, perPage:

    10) let api = APIs.Recipes.Get(paginationParams: params) SharedAPIClient.sendRequest(api) { response in switch response { case .Success(let value): value[0].cookingTime case .Failure(let value): value.message } } Enum Nested Classes Either Associated Value Protocol With Associated Type