benefited from the experiences hard- won by many other languages (…) Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list." Groovy Scala Rust C# ObjC Haskell
"some text" if a == b { println("The strings are equal") } if a.hasPrefix("some") { println("Starts with some") } if a.hasSuffix("some") { println("Endss with some") } a = "some text" b = "some text" if a == b: print("The strings are equal") if a.startswith("some"): print("Starts with some") if a.endswith("some"): print("Ends with some")
"some text" if a == b { println("The strings are equal") } if a.hasPrefix("some") { println("Starts with some") } if a.hasSuffix("some") { println("Endss with some") } a = "some text" b = "some text" if a == b: print("The strings are equal") if a.startswith("some"): print("Starts with some") if a.endswith("some"): print("Ends with some")
["TYO": "Tokyo", "DUB": “Dublin"] for (key, value) in dict { … … } var dict = new Dictionary<string, string>(); var dict2 = new Dictionary<string, string> { { "TYO", "Tokyo" }, { "DUB", "Dublin" } }; foreach(var item in dict) { var key = item.Key; var value = item.Value; }
["TYO": "Tokyo", "DUB": “Dublin"] for (key, value) in dict { … … } var dict = new Dictionary<string, string>(); var dict2 = new Dictionary<string, string> { { "TYO", "Tokyo" }, { "DUB", "Dublin" } }; foreach(var item in dict) { var key = item.Key; var value = item.Value; }
*/ public class TrustedPersistantOAuth2Session: OAuth2Session { private let keychain: KeychainWrap /** The access token. The information is read securely from Keychain. */ public var accessToken: String? { get { return self.keychain.read(self.accountId, tokenType: .AccessToken) } set(value) { if let unwrappedValue = value { let result = self.keychain.save(self.accountId, tokenType: .AccessToken, value: unwrappedValue) } } } Computed properties aerogear-ios-oauth2
*/ public class TrustedPersistantOAuth2Session: OAuth2Session { private let keychain: KeychainWrap /** The access token. The information is read securely from Keychain. */ public var accessToken: String? { get { return self.keychain.read(self.accountId, tokenType: .AccessToken) } set(value) { if let unwrappedValue = value { let result = self.keychain.save(self.accountId, tokenType: .AccessToken, value: unwrappedValue) } } } Computed properties aerogear-ios-oauth2
*/ public class TrustedPersistantOAuth2Session: OAuth2Session { private let keychain: KeychainWrap /** The access token. The information is read securely from Keychain. */ public var accessToken: String? { get { return self.keychain.read(self.accountId, tokenType: .AccessToken) } set(value) { if let unwrappedValue = value { let result = self.keychain.save(self.accountId, tokenType: .AccessToken, value: unwrappedValue) } } } Computed properties aerogear-ios-oauth2
*/ public class TrustedPersistantOAuth2Session: OAuth2Session { private let keychain: KeychainWrap /** The access token. The information is read securely from Keychain. */ public var accessToken: String? { get { return self.keychain.read(self.accountId, tokenType: .AccessToken) } set(value) { if let unwrappedValue = value { let result = self.keychain.save(self.accountId, tokenType: .AccessToken, value: unwrappedValue) } } } Computed properties aerogear-ios-oauth2
var data = jsonString.dataUsingEncoding(NSUTF8StringEncoding) let jsonObject: AnyObject! = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) // Handling JSON in Swift with optional and type casting if let personsArray = jsonObject as? NSArray { if let firstPerson = personsArray[0] as? NSDictionary { if let name = firstPerson["name"] as? NSString { println("First person name is \(name)") } } } Problem to solve: JSON SwitftyJSON
var data = jsonString.dataUsingEncoding(NSUTF8StringEncoding) let jsonObject: AnyObject! = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) // Handling JSON in Swift with optional and type casting if let personsArray = jsonObject as? NSArray { if let firstPerson = personsArray[0] as? NSDictionary { if let name = firstPerson["name"] as? NSString { println("First person name is \(name)") } } } Problem to solve: JSON SwitftyJSON // Using SwiftyJSON let json = JSON(data: data!) if let userName = json[0]["name"].string { println("First person name is \(userName)”) }
type string case ScalarString(NSString) //private type sequence case Sequence([JSON]) //private type mapping case Mapping([String: JSON]) //private type null case Null(NSError?) . . . } Enum associated values SwitftyJSON
type string case ScalarString(NSString) //private type sequence case Sequence([JSON]) //private type mapping case Mapping([String: JSON]) //private type null case Null(NSError?) . . . } Enum associated values SwitftyJSON public init(object: AnyObject) { switch object { case let number as NSNumber: self = .ScalarNumber(number) case let string as NSString: self = .ScalarString(string) case let null as NSNull: self = .Null(nil) case let array as [AnyObject]: self = .Sequence(array) case let dictionary as [String: AnyObject]: self = .Mapping(dictionary) default: self = .Null(NSError(…)) } }
{ case .Sequence(let array): if array.count > index { return array[index] } else { return .Null(NSError(…)) } default: return .Null(NSError(…)) } } } SwitftyJSON Subscript // Using SwiftyJSON let json = JSON(data: data!) if let userName = json[0]["name"].string { println("First person name is \(userName)") }
"john", "lastname": "doe", "address": { "street": "Buch Street", "poBox": 123, "city": "Glasgow", "country": "UK" } } class Person { var firstname: String? var lastname: String var address: Address? } class Address { var street: String var poBox: Int var city: String var country: String } aerogear-ios-jsonsz
"john", "lastname": "doe", "address": { "street": "Buch Street", "poBox": 123, "city": "Glasgow", "country": "UK" } } class Person { var firstname: String? var lastname: String var address: Address? } class Address { var street: String var poBox: Int var city: String var country: String } class Person: JSONSerializable { var firstname: String? var lastname: String var address: Address? required init() {} class func map(source: JsonSZ, object: Person) { object.firstname <= source["firstname"] object.lastname <= source["lastname"] object.address <= source["address"] } } aerogear-ios-jsonsz
"john", "lastname": "doe", "address": { "street": "Buch Street", "poBox": 123, "city": "Glasgow", "country": "UK" } } class Person { var firstname: String? var lastname: String var address: Address? } class Address { var street: String var poBox: Int var city: String var country: String } class Person: JSONSerializable { var firstname: String? var lastname: String var address: Address? required init() {} class func map(source: JsonSZ, object: Person) { object.firstname <= source["firstname"] object.lastname <= source["lastname"] object.address <= source["address"] } } aerogear-ios-jsonsz // serialize from json var serializer = JsonSZ() let developer:Person = serializer.fromJSON(developerJSON, to: Person.self) XCTAssertTrue(developer.firstname == "john") XCTAssertTrue(developer.lastname == "doe")
JsonSZ) { if let value = right.value { field = value as? String } } aerogear-ios-jsonsz public func <=(inout left: Int?, right: JsonSZ) { if let value = right.value { field = value as? Int } } public func <=(inout left: Bool?, right: JsonSZ) { if let value = right.value { field = value as? Bool } }
JsonSZ) { if let value: AnyObject = right.value { switch T.self { case is String.Type, is Bool.Type, is Int.Type, is Double.Type, is Float.Type: field = value as? T . . . default: field = nil return } } } aerogear-ios-jsonsz
JsonSZ) { if let value: AnyObject = right.value { switch T.self { case is String.Type, is Bool.Type, is Int.Type, is Double.Type, is Float.Type: field = value as? T . . . default: field = nil return } } } public func <=<T: JSONSerializable>(inout left: T?, right: JsonSZ) { if let value = right.value as? [String: AnyObject] { field = JsonSZ().fromJSON(value, to: T.self) } } aerogear-ios-jsonsz