Try to make something everyday with swift.
- Removing Storyboard
- Init with nib for view controller
- Adding tableview
- Registering class with
classForCoder() - Adding tableview delegate
- 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
- 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...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 workChecking platform
#if os(iOS)import CSSwiftiOS
#elseimport CSSwift
#endifisEquals 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
)}}}Fuck Swift, I am going back to Objective-C