Introducing Nest

2015-06-08 01:26:22 +08:00
 wezzard

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

Manually Install

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

Intercept messages within a subclass

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.

@import UIKit;

#import "ProductModuleName-Swift.h"

@interface MyScrollView (OverrideDelegate)
@end
@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:

Runtime Functions

Functions:

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 地址

1669 次点击
所在节点    iDev
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/196808

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX