V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
wezzard
V2EX  ›  iDev

Introducing Nest

  •  
  •   wezzard · 2015-06-08 01:26:22 +08:00 · 1657 次点击
    这是一个创建于 3462 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Github 地址

    Nest is a library which makes Foundation and Swift works more seamlessly and offers some great missing helpers, utilities in Foundation.

    How to Get Started

    Install via Cocoa Pods

    • pod "Nest.swift"
    • Add import Nest to your Swift source file

    Manually Install

    • Download or clone the project
    • Drag Nest’s Xcode project file to your workspace
    • Add Nest to your Emebed Binaries field in your target’s general page if you are building an app. Or add Nest to your Linked Frameworks and Libraries field in your target’s general page if you are building a framework.
    • Add import Nest to your Swift source file
    • You may need to add dependancies manually in the same way to your project file.
    • Enjoy your journey of Nest

    Contents

    NSProtocolInterceptor

    Circumstance:

    This class comes out of an answer I wrote on Stack Overflow: Intercept Objective-C delegate messages within a subclass. But as the original code was written in Objective-C and Swift's code-level-safety makes it become impossible that assign an object of any type to a pointer, I rewrote it in Swift and added some additional code to make it work in Swift now.

    Introduction:

    NSProtocolInterceptor is an object which masquerade itself as the protocol type(s) which you assigned at the initialization time, and intercepts message to the middle man if it could respond which originally intended to send to the receiver.

    Examples:

    Initialization

    • NSProtocolInterceptor(aProtocol: Protocol)

    • NSProtocolInterceptor(protocols: [Protocol])

    • NSProtocolInterceptor(protocols: Protocol ...)

    Intercept messages within a subclass

    • MyScrollView.swift
    class MyScrollView: UIScrollView, UIScrollViewDelegate {
        let delegateInterceptor: NSProtocolInterceptor
    
        func scrollViewDidScroll(scrollView: UIScrollView) {
            if self.delegate?.respondsToSelector("scrollViewDidScroll:") == true {
                self.delegate?.scrollViewDidScroll?(scrollView)
            }
        }
    
        required init(coder aDecoder: NSCoder) {
            delegateInterceptor = NSProtocolInterceptor(aProtocol: UIScrollViewDelegate.self)
            super.init(coder: aDecoder)
            delegateInterceptor.middleMan = self
            super.delegate = delegateInterceptor as? UIScrollViewDelegate
        }
    
        override init(frame: CGRect) {
            delegateInterceptor = NSProtocolInterceptor(aProtocol: UIScrollViewDelegate.self)
            super.init(frame: frame)
            delegateInterceptor.middleMan = self
            super.delegate = delegateInterceptor as? UIScrollViewDelegate
        }
    }
    

    For UIKit bridging header claims UIScrollView's delegate property as unowned(unsafe) and doesn't mark UIScrollViewDelegate up as a class only protocol, we cannot override the delegate property in Swift for now. So let's override it in Objective-C.

    • MyScrollView+OverrideDelegate.h
    @import UIKit;
    
    #import "ProductModuleName-Swift.h"
    
    @interface MyScrollView (OverrideDelegate)
    @end
    
    • MyScrollView+OverrideDelegate.m
    @import Nest;
    
    #import "MyScrollView+OverrideDelegate.h"
    
    @implementation MyScrollView (OverrideDelegate)
    - (id <UIScrollViewDelegate>)delegate {
        return (id <UIScrollViewDelegate>)self.delegateInterceptor.receiver;
    }
    
    - (void)setDelegate:(id <UIScrollViewDelegate>)delegate {
        super.delegate = nil;
        self.delegateInterceptor.receiver = delegate;
        super.delegate = (id <UIScrollViewDelegate>)self.delegateInterceptor;
    }
    @end
    

    NSReuseCenter & NSReusable

    Circumstance:

    Reusing is an usual way to optimize a program's performance. This class it designed for making reusing simpler.

    Introduction:

    NSReuseCenter offers an enqueue and dequeue mechanism for objects conform to the NSReusable protocol.

    Examples:

    Initializations

    let theReuseCenter = NSReuseCenter<AParticularReusable>()
    

    Enqueue an unused object

    theReuseCenter.enqueueUnused(anUnusedObject)
    

    Dequeue a reusable object for a particular reuse identifier

    theReuseCenter.dequeueReusableWithReuseIdentifier(aReuseIdentifier)
    

    Query all reusable objects for a particular reuse identifier

    theReuseCenter.reusableForReuseIdentifier(aReuseIdentifier)
    

    Collection Functions

    Circumstance:

    There might be a situation you had encountered before where you wanted to find an NSObjectProtocol conformed object in a protocol-constrained collection and Swift didn't suppose your NSObjectProtocol conformed object implements the Equatable protocol which made you unable to use the find function in Swift standard library. These collection of functions assume that any NSObjectProtocol conformed object has a clear understand on isEqual: function and use that function to evaluate equality between objects.

    Functions:

    • Check an NSObjectProtocol conformed object is contained in a collection of NSObjectProtocol conformed objects.

      NSContains<C where C: CollectionType, C.Generator.Element: NSObjectProtocol>(domain: C, element: C.Generator.Element) -> Bool

    • Find the index of an NSObjectProtocol conformed object in a collection of NSObjectProtocol conformed objects.

      NSFind<C : CollectionType where C.Generator.Element: NSObjectProtocol>(domain: C, value: C.Generator.Element) -> C.Index?

    • Calculate intersected objects in two collections of NSObjectProtocol conformed objects.

      NSIntersected<C : ExtensibleCollectionType where C.Generator.Element : NSObjectProtocol>(collectionA: C, collectionB: C) -> C

    • Evaluate differences between two collections of NSObjectProtocol conformed objects.

      NSDiff<Seq: SequenceType where Seq.Generator.Element: NSObjectProtocol>(from fromSequence: Seq?, to toSequence: Seq?, differences: SequenceDifference, unchangedComparator: ((Seq.Generator.Element, Seq.Generator.Element)->Bool) = default, usingClosure changesHandler: (change: SequenceDifference, fromElement: (index: Int, element: Seq.Generator.Element)?, toElement: (index: Int, element: Seq.Generator.Element)?) -> Void)

    • Remove an NSObjectProtocol conformed object in a collection of NSObjectProtocol conformed objects.

      NSRemove<C : RangeReplaceableCollectionType where C.Generator.Element : NSObjectProtocol, C.Index: protocol<Comparable, BidirectionalIndexType>> (inout collection: C, elements: C) -> C

    Runtime Functions

    Functions:

    • Check if selector belongs to a protocol.

      sel_belongsToProtocol(aSelector: Selector, aProtocol: Protocol) -> Bool

    • Swizzle a selector with an implementation on a class. The original implementation would be connected with a selector which consists of the given prefix and its original selector.

      class_swizzleClass(aClass: AnyClass, selector: Selector, withImplementation: IMP, selectorPrefix: String) -> Bool

    Conformity of NSDate to Comparable

    Circumstance:

    I made NSDate conforms to Comparable to get avoid of comparing two NSDate objects by using its compare: function.

    Example:

    let now = NSDate()
    let tenSecondsBefore = now.dateByAddingTimeInterval(-10)
    
    if now > tenSecondsBefore {
        println("Now is later than ten seconds before")
    }
    

    Search All Occurrence for Given String in NSString

    Circumstance:

    Search all occurrence for given string in an NSString without using regular expression.

    Example:

    Search string in the whole string with the current locale

    let aString = "This is a string"
    let aStringInNSString = aString as NSString
    
    let occurrences = aStringInNSString.rangesOfString("s", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil)
    

    NSRangeMake

    Example:

    let anNSRange = NSRangeMake(location: 0, length: 10)
    

    NSError Initialization Conveniences for Using CoreData

    Example:

    Combine two errors

    let anError = NSError(error: anError, anotherError: anOtherError)
    

    Combine an error in a pointer with another error

    let anError = NSError(errorPointer:anErrorPointer, secondError: secondError)
    

    Combine an array of errors

    let anError = NSError(errors: errors)
    

    Dependancies

    Github 地址

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1397 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 23:44 · PVG 07:44 · LAX 15:44 · JFK 18:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.