ios - In Swift, how can I quickly set my instance variables? -


public class user {     var id: int     var fb_id: string?     var first_name: string?     var last_name: string?     var age: int?     var distance: int?     var login_at_pretty: string?     var login_at: int?     var profile: profile?     init(id: int, fb_id: string?, first_name: string?, last_name: string?, age: int?, distance: int?, login_at_pretty: string?, login_at: int?, profile: profile?){          self.id = id         if let fb_id = fb_id {             self.fb_id = fb_id         }         if let first_name = first_name {             self.first_name = first_name         }         if let last_name = last_name {             self.last_name = last_name         }         if let age = age {             self.age = age         }         if let distance = distance {             self.distance = distance         }         if let login_at_pretty = login_at_pretty {             self.login_at_pretty = login_at_pretty         }         if let login_at = login_at {             self.login_at = login_at         }         if let profile = profile {             self.profile = profile         }     } } 

is quickest way it? feel i'm on typing.

for class, you're doing lot of if-let statements don't provide useful. ie:

   if let fb_id = fb_id {         self.fb_id = fb_id    } 

fb_id , self.fb_id optionals, binding isn't necessary. do:

self.fb_id = fb_id 

on note however, if you're not planning on subclassing, using struct provide memberwise initializer automagically:

public struct user {     var id: int     var fb_id: string?     var first_name: string?     var last_name: string?     var age: int?     var distance: int?     var login_at_pretty: string?     var login_at: int?     var profile: profile? } 

check out memberwise initializers structure types in swift docs


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -