Skip to content

Latest commit

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Learing swift

Try to make something everyday with swift.

iPhone Project with swift:

  • Removing Storyboard
  • Init with nib for view controller
  • Adding tableview
  • Registering class with classForCoder()
  • Adding tableview delegate

Trying Request

  • URL with string
  • Playground cannot make request
  • Block

Implementing PokemonKit a wrapper class of Pokeapi-v2 written in swift

  • Writing closeure, block in swift public func fetchBerryList(completion: (result: NSArray) -> Void, failure: (error: NSError) -> Void)
  • Define dictionary [NSLocalizedDescriptionKey: "Berry List Error"]
  • Tools for mapping objects Link

Trying PromiseKit

  • Write a promise
publicfunc fetchBerryList()->Promise<[PKMBaseObject]>{returnPromise{ fulfill, reject inletURL= baseURL +"/berry"Alamofire.request(.GET, URL).responseArray{(response:Response<[PKMBaseObject],NSError>)inif(response.result.isSuccess){fulfill(response.result.value!)}else{reject(response.result.error!)}}};
}
  • Writing test case with swift
  • Testing asnc calls
letasyncExpectation=expectationWithDescription("Fetch berries")PokemonKit.fetchBerryList().then{ response in
asyncExpectation.fulfill()}.error{ err inXCTFail("Should not failed with \(err)")}self.waitForExpectationsWithTimeout(30){(err)->VoidinXCTAssertNil(err,"Something went wrong")}
  • Fixing missing closure for AnyPromise
PokemonKit.fetchBerry("1").then { response ->Void in // this is the fix (-> Void in)
...continue...

Loading cocoapods library bundle resources

Add Resource path in podspec

s.resource_bundles={'CSSwift'=>['Pod/Assets/*.js']}

Load it with NSBundle

func loadCssJs(){
cssJs =JSContext()letpodBundle=NSBundle(forClass:self.classForCoder)iflet bundleURL = podBundle.URLForResource("CSSwift", withExtension:"bundle"){iflet bundle =NSBundle(URL: bundleURL){letcssJsPath= bundle.pathForResource("css", ofType:"js")do{letcontent=tryString(contentsOfFile: cssJsPath!)
cssJs.evaluateScript(content)}catch{}}else{assertionFailure("Could not load the bundle")}}else{assertionFailure("Could not create a path to the bundle")}}

For-in loop

fordictin jsResult{
result.append(CSSModel(infoDict: dict as![String:AnyObject]))}

Init functions

convenienceinit(infoDict:[String:AnyObject]!){self.init()
rules =infoDict["rules"]! as?[AnyObject]
selector =infoDict["selector"]! as?String}

Adding variable in JavascriptCore

cssJs.setObject(cssString, forKeyedSubscript:"cssValue")
// don't use cssJs.evaluateScript("var cssString = '\(cssString)';"), line break will not work

Checking platform

#if os(iOS)import CSSwiftiOS
#elseimport CSSwift
#endif

isEquals in Swift

// Useful when compare arrays of objects
overridepublicfunc isEqual(object:AnyObject?)->Bool{guardlet rhs = object as?CSSRuleModelelse{returnfalse}letlhs=selfreturn(lhs.ruleName == rhs.ruleName &&
lhs.ruleContent == rhs.ruleContent)}

Assoicate Object

privatestructAssociatedKeys{staticvarCSSisUrlAssociationKey:UInt8=0}publicvarisUrl:Bool!{get{guardlet result =objc_getAssociatedObject(self,&AssociatedKeys.CSSisUrlAssociationKey)as?Boolelse{returnfalse}return result
}set{iflet newValue = newValue {objc_setAssociatedObject(self,&AssociatedKeys.CSSisUrlAssociationKey,
newValue,.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)}}}

Day X

Fuck Swift, I am going back to Objective-C

About

Learning swift

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages