Coder Social home page Coder Social logo

lxfoundation's Introduction

#LXFoundation

LXFoundation is a set of basic container classes mean to replace Cocoa NS prefix containers such as NSMutableArray, NSMutableDictionary. It will handle invalid value silently rather than throw an exception to crash your app.

All classes in LXFoundation are ARC and non-ARC ready.

##About performance

LXFoundation builds on top of Core Foundation Classes, their performance are almost identical to NS-Containers.

##Basic usage

Drag LXFoundation into your xcode project, and compile. If you don't want to change your existing project code. Then you can simply add following lines to your project prefix.pch:

#import "LXFoundation.h"
#define NSMutableArray LXMutableArray
#define NSMutableDictionary LXMutableDictionary

You can simply comment these line and fallback to NS-containers.

###LXMutableDictionary

LXMutableDictionary is same as NSMutableDictionary except it will not throw an exception if the value or key is nil object. This will make your code more robust and easy to debug.

Beware, if some part of your code depends on default NSMutableDictionary exception to work, this should be handled more carefully.

//Init LXMutableDictionary
LXMutableDictionary *dic=[LXMutableDictionary dictionary];
LXMutableDictionary *dic=[LXMutableDictionary dictionaryWithCapacity:10];
LXMutableDictionary *dic=[[LXMutableDictionary alloc] init];
LXMutableDictionary *dic=[[LXMutableDictionary alloc] initWithCapacity:10];

//Add Object
[dic setObject:@"obj1" forKey:@"key1"];
[dic setObject:@"obj2" forKey:@"key1"];
[dic setObject:[LXMutableDictionary dictionaryWithObjectsAndKeys:
				@"obj11", @"key11",
				@"obj12", @"key12",
				nil]
		forKey:@"key1"];

//Fast Enumerator
for(id key in dic){
	NSLog(@"dic[%@]=%@", key, [dic objectForKey:key]);
}
NSEnumerator *keyEnumerator=[dic keyEnumerator];
id key;
while ( key =[keyEnumerator nextObject] ) {
	NSLog(@"dic[%@]=%@", key, [dic objectForKey:key]);
}

//Save and read LXMutableDictionary
NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/testDic.plist"];
[dic writeToFile:path atomically:NO];
dic=[LXMutableDictionary dictionaryWithContentsOfFile:path];

NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/testDic.dat"];
[NSKeyedArchiver archiveRootObject:dic toFile:path];
dic=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

###LXMutableArray

LXMutableArray is same as NSMutableArray except it will not throw an exception if the value is nil object. This will make your code more robust and easy to debug.

Beware, if some part of your code depends on default NSMutableArray exception to work, this should be handled more carefully.

//Init LXMutableArray
LXMutableArray *array=[LXMutableArray array];
LXMutableArray *array=[LXMutableArray arrayWithCapacity:10];
LXMutableArray *array=[[LXMutableArray alloc] init];
LXMutableArray *array=[[LXMutableArray alloc] initWithCapacity:10];

//Add Object
[array addObject:@"obj1"];
[array addObject:[NSNumber numberWithInteger:123]];
[array addObject:[LXMutableDictionary dictionaryWithObjectsAndKeys:
				  @"obj11", @"key11",
				  @"obj12", @"key12",
				  nil]];
				  

//Fast Enumerator
for(id obj in array){
	NSLog(@"obj=%@", obj);
}

//Save and read LXMutableArray
NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/array.plist"];
[array writeToFile:path atomically:NO];
array = [LXMutableArray arrayWithContentsOfFile:path];

NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/array.dat"];
[NSKeyedArchiver archiveRootObject:array toFile:path];
array=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

lxfoundation's People

Contributors

keefo avatar

Stargazers

PhilCai avatar YoungShook avatar  avatar 张群 avatar Xing He avatar Eric avatar x avatar mactive avatar  avatar Andy Chang avatar dai yun avatar  avatar Allen Hsu avatar mayulu avatar  avatar Aaron ttgb avatar Nicholas Tau avatar Xin Zhou avatar lukewcn avatar  avatar  avatar  avatar TONy.W avatar Wutian avatar

Watchers

mayulu avatar  avatar James Cloos avatar

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.