2012年5月15日 星期二

CSV 轉換成 plist (Property List) 檔

http://stackoverflow.com/questions/1546007/convert-excel-document-xls-to-a-plist

下載 Plist Converter1.0

或是 使用 cCSVParse 這個 lib

套用底下程式碼


 CSVParser *parser = [CSVParser new];
[parser openFileWithPath:pathAsString];
NSMutableArray *csvContent = [parser parseFile];
[parser closeFile];
if (pathAsString != nil)
{

        NSArray *keyArray = [csvContent objectAtIndex:0];

        NSMutableArray *plistOutputArray = [NSMutableArray array];

        NSInteger i = 0;

        for (NSArray *array in csvContent)
        {



                NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

                NSInteger keyNumber = 0;

                for (NSString *string in array)
                {

                        [dictionary setObject:string forKey:[keyArray objectAtIndex:keyNumber]];

                        keyNumber++;

                }

                if (i > 0)
                {
                        [plistOutputArray addObject:dictionary];
                }

                i++;

        }

        NSMutableString *mutableString = [NSMutableString stringWithString:pathAsString];
        [mutableString replaceOccurrencesOfString:@".csv" withString:@".plist" options:nil range:NSMakeRange([mutableString length]-4, 4)];

        NSURL *url = [NSURL fileURLWithPath:mutableString];


        [plistOutputArray writeToURL:url atomically:YES];

CSV 轉檔到 Core Data

http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html

Download Sample

2012年5月14日 星期一

Core Data 必看的基礎文章

Core Data Simple Example

Core Data Basic Concept and Instance Methods

Core Data Relationship

全部出自於 Chris的部落格 請參考


還有這張圖


Core Data CRUD 新增、刪除、修改、查詢 (create save delete update select read query)


1. 插入 新增 存檔 create add save

AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [app managedObjectContext];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"entityname" inManagedObjectContext:context];
[newManagedObject setValue:value forKey:@"propertyname"];
NSError *error; if (![context save:&error]) {
 
    // Handle the error…
 
}

2. 查詢 query select read


NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Hero" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc]
initWithKey:@"secretIdentity" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]
initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSArray *objecs = [context executeFetchRequest: fetchRequest error:&error];

3. 刪除 delete destroy remove

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
[context deleteObject:[objecs objectIndex:index];
// Save the context.
NSError *error; if (![context save:&error]) {
 
    // Update to handle the error appropriately.
 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
 
    exit(-1); // Fail
 
}

//屬性

NSManagedObject *managedObject;
NSString *keypath;
NSString *labelString;
NSString *currentValue = [self.managedObject valueForKeyPath:self.keypath];
NSEntityDescription *ed = [self.managedObject entity];
NSDictionary *properties = [ed propertiesByName];
NSAttributeDescription *ad = [properties objectForKey:self.keypath];
NSString *defaultValue = nil;
if (ad != nil)
defaultValue = [ad defaultValue];
//core data relation  查詢 或 修改
NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName: @"Person" inManagedObjectContext:thePerson.managedObjectContext];
NSMutableSet *children = [person mutableSetValueForKey:@"children"]; //查詢,可修改
[children addObject:child];
[children removeObject:childToBeRemoved];
[[children managedObjectContext] deleteObject:childToBeRemoved]; //真正的刪除


NSSet *children = [person valueForKey:@"children"]; //查詢,不可修改
for (NSManagedObject *oneChild in children) {
 
    // do something
 
}

補充:

1 是否將圖片存儲到Core Data中,以及NSData如何存儲的一些規則

First, always store your images in a usable format such as PNG or JPEG instead of NSData. This will save you a lot of headaches.

Second, the rule for storing binary data is:


< 100kb store in the same table as the relevant data
< 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
> 1mb store on disk and reference it inside of Core Data

要懂 Core Data 之前, 要先懂 KVO & KVC

引入朋友 CFC 的文章
http://hechien.posterous.com/kvo-01


KVC Key-Value Coding


  • setValue forKey
  • salueForKey

有用過 直譯式語言應該很清楚 KVC
Java 就有如 Reflection 機制


KVO Key-Value Observing

addObserver:forKeyPath:options:context:

MVC 架構 似乎很需要這樣子的通知方式,

有點忘記是否跟 GCD (Grand Central Dispatch ) 有什麼關係。

2012年5月3日 星期四

要怎麼換code signing identity

要怎麼換code signing identity?
認證都有下載了,裝上去了,
但是灰白的................點不到




原因是:
iOS Application Target裡的Bundle Identifier沒改到..................
要記得改呦!~