美文网首页
https://github.com/nickoneill/th

https://github.com/nickoneill/th

作者: LV大树 | 来源:发表于2017-08-29 14:39 被阅读4次

    here are copys from the topic of title.

    1.api-client网络请求类
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
    let request = NSURLRequest(URL: NSURL(string: "http://yourapi.com/endpoint")!)

    let task: NSURLSessionDataTask = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
    if let data = data {
    let response = NSString(data: data, encoding: NSUTF8StringEncoding)
    print(response)
    }
    }.resume()

    2.background-threads.txt 后台处理然后主线程UI刷新
    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {
    // do some task
    dispatch_async(dispatch_get_main_queue()) {
    // update some UI
    }
    }

    3.completion-handlers.txt 完成处理block
    func hardProcessingWithString(input: String, completion: (result: String) -> Void) {
    ...
    completion("we finished!")
    }

    4.dependency-injection.txt 依赖注入?其实我不懂这一块。
    class PreparedViewController: UIViewController {
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let dest = segue.destinationViewController

        let prepProps = Mirror(reflecting: dest).children.filter { ($0.label ?? "").hasPrefix("prepCtx") }
        for prop in prepProps {
            let selfProps = Mirror(reflecting: self).children.filter { ($0.label ?? "") == prop.label }
            if let sameProp = selfProps.first, childObject = sameProp.value as? AnyObject, label = prop.label {
                dest.setValue(childObject, forKey: label)
            }
        }
    }
    

    }

    5.documents-directory.txt 文件与文件夹操作。
    let swiftArray = NSArray(contentsOfFile: filePath) as? [String]
    if let swiftArray = swiftArray {
    // now we can use Swift-native array methods
    find(swiftArray, "findable string")
    // cast back to NSArray to write
    (swiftArray as NSArray).writeToFile(filePath, atomically: true)
    }

    6.gesture-recognizers.txt 手势处理。
    override func viewDidLoad() {
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
    self.view.addGestureRecognizer(gestureRecognizer)
    }

    func handleTap(gestureRecognizer: UIGestureRecognizer) {
    let alertController = UIAlertController(title: nil, message: "You tapped at (gestureRecognizer.locationInView(self.view))", preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "Dismiss", style: .Cancel, handler: { _ in }))
    self.presentViewController(alertController, animated: true, completion: nil)
    }

    7.guard-statements.txt guard声明。区别OC.
    func tappedSubmitButton() {
    guard let name = nameField.text where isValid(name) else {
    show("name failed validation")
    return
    }

    submit(name)
    

    }

    8.ibaction-iboutlet.txt outlet 与ibaction
    class MyViewController: UIViewController {
    @IBOutlet weak var likeButton: UIButton?
    @IBOutlet weak var instruction: UILabel?

    @IBAction func likedThis(sender: UIButton) {
    ...
    }
    }

    9.json-serialization.txt JSON序列化
    let data: NSData = ...some data loaded...
    let jsonError: NSError?
    let decodedJson = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError!) as Dictionary<String, AnyObject>
    if !jsonError {
    print(decodedJson["title"])
    }

    10.kill-your-viewdidload.txt what fuck of this???????? ..small peace of OC such like a = ({b});
    class ViewController: UIViewController {
    let topView: UIView = {
    let view = UIView()
    view.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
    view.backgroundColor = UIColor.redColor()
    return view
    }()
    }
    11.method-signatures.txt 方法声明。
    func repeatThis(name: String, andDoItThisManyTimes times: Int) {
    for i in 0..<times {
    print(name)
    }
    }

    repeatThis("swift", andDoItThisManyTimes: 3)

    12.remote-notifications.txt 远程通知。
    UIApplication.sharedApplication().registerForRemoteNotifications()

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
    print("Got token data! (deviceToken)")
    }

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!) {
    print("Couldn't register: (error)")
    }

    13.singletons 单例。
    class SomeManager {
    static let sharedInstance = SomeManager()

    func doAThing() {
        // thing
    }
    

    }

    SomeManager.sharedInstance.doAThing()
    14.sorting-arrays.txt 排序。
    var numbers = [0, 2, 3, 5, 10, 2]
    numbers.sort {
    return $0 < $1
    }

    15.string-formatting.txt 字符串格式化。
    let timeString = String(format: "The current time is %02d:%02d", 10, 4)

    16.table-state.txt 枚举。
    enum TableState {
    case Loading
    case Failed
    case Items([String])
    }

    17.type-inference.txt 类型参考。
    let myCompany: Dictionary<String, AnyObject> = [
    "employees": [
    "employee 1": ["attribute": "value"],
    "employee 2": ["attribute": "value"],
    "employee 3": ["attribute": "value"],
    "employee 4": ["attribute": "value"],
    "employee 5": ["attribute": "value"]
    ]
    ]

    相关文章

      网友评论

          本文标题:https://github.com/nickoneill/th

          本文链接:https://www.haomeiwen.com/subject/gvpodxtx.html