Skip to content

Instantly share code, notes, and snippets.

@maxchuquimia
Last active January 12, 2016 00:59
Show Gist options
  • Save maxchuquimia/a2a1ddc8849f25c8cf85 to your computer and use it in GitHub Desktop.
Save maxchuquimia/a2a1ddc8849f25c8cf85 to your computer and use it in GitHub Desktop.
Things that don't work in Swift but should
//MARK: instancetype
//Xcode 7A176x
extension UIViewController {
class func fromStoryboard(named name: String) -> Self {
let storyboard = UIStoryboard(name: name, bundle: NSBundle.mainBundle())
let classname = NSStringFromClass(self) as NSString //Error: Type 'Self" does not conform to protocol 'AnyObject'
return storyboard.instantiateViewControllerWithIdentifier(classname.pathExtension)
}
}
//Instead (thanks to http://stackoverflow.com/a/27112385/1153630) :
extension UIViewController {
class func fromStoryboard(named name: String) -> Self {
return internalFromStoryboard(named: name)
}
private class func internalFromStoryboard<T>(named name: String) -> T {
let storyboard = UIStoryboard(name: name, bundle: NSBundle.mainBundle())
let classname = entityName()
return storyboard.instantiateViewControllerWithIdentifier(classname) as! T
}
private class func entityName() -> String {
let classname = NSStringFromClass(self) as NSString
return classname.pathExtension
}
}
//MARK: Lazy Instantiation (7A176x)
lazy private var backgroundImage = UIImage(named: "test") //Error: cannot invoke 'init' with an argument list of type '(named: String)'
//MARK: Classes that conform to Protocols are not possible in Swift (7A192o)
@property(..) UIView<SomeProtocol> *myView
//MARK: JSON Serialization of backslash goes crazy (GM 7A218)
3> import Foundation
4> let timeInterval = Int(NSDate().timeIntervalSince1970 * 1000)
timeInterval: Int = 1442192636505
5> let s = "\\/Date(\(timeInterval))\\/"
s: String = "\\/Date(1442192636505)\\/"
6> print(s)
\/Date(1442192636505)\/
7> let params = ["Date": s]
params: [String : String] = 1 key/value pair {
[0] = {
key = "Date"
value = "\\/Date(1442192636505)\\/"
}
}
8> print(NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted))
<7b0a2020 22446174 6522203a 20225c5c 5c2f4461 74652831 34343231 39323633 36353035 295c5c5c 2f220a7d>
9> let data = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted)
data: NSData = 44 bytes
10> let newString = String(data:data, encoding:NSUTF8StringEncoding)
newString: String? = "{\n \"Date\" : \"\\\\\\/Date(1442192636505)\\\\\\/\"\n}"
11> print(newString)
Optional("{\n \"Date\" : \"\\\\\\/Date(1442192636505)\\\\\\/\"\n}")
12> print(newString!)
{
"Date" : "\\\/Date(1442192636505)\\\/"
}
13>
//Can't have a [AnyObject] public property that is immutable (7C68)
@property (..., copy) NSArray *test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment