Giter Site home page Giter Site logo

youknowone / foundationextension Goto Github PK

View Code? Open in Web Editor NEW
121.0 10.0 22.0 5.94 MB

Foundation/Cocoa/UIKit extension kit. Reference document:

Home Page: http://youknowone.github.io/FoundationExtension

License: Other

Objective-C 95.95% Python 2.81% Objective-C++ 0.21% Makefile 0.03% Ruby 0.93% Rich Text Format 0.07%
objective-c extension shortcuts snippets cocoapods

foundationextension's Introduction

@mainpage FoundationExtension

Build Status

This library includes small Cocoa/UIKit extensions. This library does not includes high-level data structure, algorithm or frameworks, but collection of code snippets.

  • Many common snippets in a method call.
  • Looks like native foundation methods - It follows Apple Coding Guideline and Foundation naming convention.

See document on [Github] (http://youknowone.github.com/FoundationExtension)

How to use

  • Compiled library
    1. Build project
    2. Add FoundationExtension or UIKitExtension target as dependency
  • Directy source
    1. Add files what you need to your project
  • CocoaPod ~> 1.7.5
    1. Visit and follow http://cocoapods.org/

If your compiler is gcc or old clang, add '-force_load' to static library.

Download for editing

git clone git://github.com/youknowone/FoundationExtension.git
cd FoundationExtension
git submodule update --init

Why useful

Make your code short! Do not allow evil objc to make your code verbose. This library includes many shortcuts for common work.

NSData from URL

Foundation

NSString *URLString = [NSSring stringWithFormat:@"http://"HOST_URL"/api/%@", key];
NSURL *URL = [NSURL URLWithString:URLString];

FoundationExtension

NSURL *URL = [[@"http://"HOST_URL"/api/%@" format:key] URL];

@see @ref NSString(Shortcuts) @see @ref NSString(NSURL)

iPhone MAC Address

Foundation

  • No way.

FoundationExtension

[[UIDevice currentDevice] MACAddress]

@see @ref UIDevice(Shortcuts)

performSelector, with 3 object

Foundation

  • No way. You should use <objc/runtime.h>

FoundationExtension

[obj performSelector:sel withObject:o1 withObject:o2 withObject:o3];

@see @ref NSObject(ObjCRuntime)

Get NSData from post request

Foundation

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:@"field1=value1"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];

FoundationExtension

NSData *data = [NSData dataWithContentsOfURL:URL postBody:@{@"field1":@"value1"} encoding:NSUTF8StringEncoding];

@see @ref NSData(NSURLRequest)

Get NSData from Multipart Form POST

Foundation

  • No way.

FoundationExtension

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMultiPartFormPostBody:@{@"filename":data} encoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithContentOfURLRequest:request];

@see NSURLRequestAdditions.h

Truncate strings in array

Foundation

NSMutableArray *newArray = [NSMutableArray array];
for (NSString *s in array) {
    [newArray addObject:[s substringToIndex:20]];
}

FoundationExtension NSArray

NSArray *newArray = [array arrayByMappingOperation:^(NSString *obj){ [obj substringToIndex:20]; }];

FoundationExtension NSMutableArray

[array map::^(NSString *obj){ [obj substringToIndex:20]; }];

@see @ref Map/Filter/Reduce @see NSAFunctional.h

Get a class name

Foundation

NSString *className = [NSString stringWithUTF8String:class_getName(obj.class)];

FoundationExtension

NSString *className = obj.class.name;

@see @ref NSObject(ObjCRuntime) @see @ref NSObject(ObjCRuntimeClass)

Get hexadecimal value from base 16 string

Foundation

int value;
sscanf(string.UTF8String, "%x", &value);

FoundationExtension

NSInteger value = [string hexadecimalValue];

@see @ref NSData(Serialization)

How about base 12 string?

Foundation

  • Why should foundation has this?

FoundationExtension

NSInteger value = [string integerValueBase:12];

@see @ref NSString(Evaluation)

md5 hash

Foundation

unsigned char hashedChars[CC_MD5_DIGEST_LENGTH];
CC_MD5([data bytes], (CC_LONG)[self length], hashedChars);
NSMutableString *result = [[NSMutableString alloc] init];
for ( int i = 0; i<CC_MD5_DIGEST_LENGTH; i++ ) {
    [result appendFormat:@"%02x", *(hashedChars+i)];
}

FoundationExtension

NSString *result = [data digestStringByMD5];

@see @ref NSData(CommonCrypto)

plist dictionary decode from HTTP post request

Foundation

  • Get data from NSURLRequest. Ehh.. so what can I do now? (Use NSPropertyListSerialization)

FoundationExtension

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPPostBody:@{@"key1":@"value1"} encoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithContentOfURLRequest:request];
NSDictionary *dictionary = [NSDictionary dictionaryWithData:data];

@see @ref NSMutableURLRequest(HTTPMethod) @see @ref NSData(NSURLRequest) @see @ref NSDictionary(NSData)

UIColor from HTML color code

UIKitExtension

UIColor *color = [UIColor colorWithHTMLExpression:@"#f0f0f0"];

@see @ref UIColor(HTMLColor)

Change implementation to new one

FoundationExtension

[class methodObjectForSelector:@selector(method1)].implementation
  = [class methodObjectForSelector:@selector(method2)].implementation;
// now [obj method1] equals [obj method2]

@see NSAMethod

Retrieve accessibility for private variable.

FoundationExtension

@interface Secret: NSObject { @private id _attr; } @end // #1 remember the '_attr'
// Hack the Secret!
@interface Secret (Accessor)
@property
    (nonatomic, retain) id attr; // #2 remember the 'attr'
@end
@implementation Secret (Accessor)
NSAPropertyGetter(attr, "_attr") // #2, #1 to create getter
NSAPropertyRetainSetter(setAttr, "_attr") // #2, #1 to create getter
@end

@see NSObject(ObjCRuntime)

For more

See the document! [http://youknowone.github.com/FoundationExtension] (http://youknowone.github.com/FoundationExtension)

foundationextension's People

Contributors

hanswannop avatar krusek avatar philipkimmey avatar redetection avatar sarsonj avatar tadeuzagallo avatar youknowone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

foundationextension's Issues

UIImageAdditions imageByResizingToSize returns a blank image

UIImageAdditions imageByResizingToSize returns a blank image, here's my code:

#import <FoundationExtension/UIImageAdditions.h>

...

UIImage *compassImage = [UIImage imageNamed:@"compass.png"];
UIImage *compassImageThumb = [compassImage imageByResizingToSize:CGSizeMake(20.0, 20.0)];
UIBarButtonItem *compassButtonItem = [[UIBarButtonItem alloc] initWithImage:compassImageThumb style:UIBarButtonItemStyleBordered target:self action:@selector(compassButtonPressed:)];
self.navigationItem.rightBarButtonItem = compassButtonItem;

This is what I got:

UIImage

The compass.png image:

UIImage

BTW, using UIGraphicsBeginImageContext to resize the image using a CGSize works!, the code:

UIImage *compassImage = [UIImage imageNamed:@"compass.png"];
CGSize newSize = CGSizeMake(20.0, 20.0);
UIGraphicsBeginImageContext(newSize);
[compassImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *compassImageThumb = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Prefix category methods on core Objective C classes.

Installed UI7Kit and thus FoundationExtension earlier but got a weird hard-to-debug crash. Took a while to figure out but finally tracked down the culprit to

@implementation NSMutableArray (Functional)

- (void)map:(NSAObjectUnaryOperator)mapper {
    NSUInteger count = self.count;
    for (NSUInteger i = 0; i < count; i++) {
        self[i] = mapper(self[i]);
    }
}

This category implementation overrode a previous category I have on NSMutableArray (though BlocksKit), which caused the crash at runtime. NSMutableArray is such a generic class and map: is such a generic method signature that it's almost guaranteed to run into method name clash problems for any projects of reasonable size and complexity, making FoundationExtension (and by extension UI7Kit) unusable :(. Could take an approach similar to MagicalRecord (https://github.com/magicalpanda/MagicalRecord/blob/develop/MagicalRecord/CoreData%2BMagicalRecord.h#L14), define a macro if you want the shorthand version offered by FoundationExtension, otherwise prefix all category methods on core Objective C classes with fe_ for Foundation Extension.

CABasicAnimation not work

I imoprt FoundationExtension use pod, But my CABasicAnimation not work, after I look into it, I found if I delete the method + (NSAMethod *)methodForSelector:(SEL)selector in the NSObject.m , CABasicAnimation work fine.

CocoaPod

Any plans to release a CocoaPod?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.