SORU
14 NİSAN 2009, Salı


Nasıl veri yapısı okumak için .NSArray plist dosyası

Veri yapısı aşağıdaki el ile oluşturma.

NSDictionary* league1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Barclays Premier League", @"name",
    			 @"Premier League", @"shortname",
    			 @"101", @"id", nil];
NSDictionary* league2 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Coca-Cola Championship", @"name",
    			 @"Championship", @"shortname",
    			 @"102", @"id", nil];
NSDictionary* league3 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish Premier League", @"name",
    			 @"SPL", @"shortname",
    			 @"201", @"id", nil];
NSDictionary* league4 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Champions League", @"name",
    			 @"UCL", @"shortname",
    			 @"501", @"id", nil];

contentArray = [[NSArray alloc] initWithObjects:

    			[[NSDictionary alloc] initWithObjectsAndKeys: @"English", @"category", [[NSArray alloc] initWithObjects: league1, league2, nil], @"leagues", nil],
    			[[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish", @"category", [[NSArray alloc] initWithObjects: league3, nil], @"leagues", nil],
    			[[NSDictionary alloc] initWithObjectsAndKeys: @"Tournaments", @"category", [[NSArray alloc] initWithObjects: league4, nil], @"leagues", nil],
    			nil];

[league1 release];
[league2 release];
[league3 release];
[league4 release];

Ancak, bu eğer bir dosyadan okuma olsaydı daha iyi olurdu diye düşündüm. Dosya Lig yarattım.aşağıdaki yapıya sahip olan plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
    	<dict>
    		<key>category</key>
    		<string>English</string>
    		<key>leagues</key>
    		<array>
    			<dict>
    				<key>name</key>
    				<string>Barclays Premier League</string>
    				<key>shortname</key>
    				<string>Premier League</string>
    				<key>id</key>
    				<string>101</string>
    			</dict>
    			<dict>
    				<key>name</key>
    				<string>Coca-Cola Championship</string>
    				<key>shortname</key>
    				<string>Championship</string>
    				<key>id</key>
    				<string>102</string>
    			</dict>
    		</array>
    	</dict>
    	<dict>
    		<key>category</key>
    		<string>Scottish</string>
    		<key>leagues</key>
    		<array>
    			<dict>
    				<key>name</key>
    				<string>Scottish Premier League</string>
    				<key>shortname</key>
    				<string>SPL</string>
    				<key>id</key>
    				<string>201</string>
    			</dict>
    		</array>
    	</dict>
    	<dict>
    		<key>category</key>
    		<string>Tournaments</string>
    		<key>leagues</key>
    		<array>
    			<dict>
    				<key>name</key>
    				<string>Champions League</string>
    				<key>shortname</key>
    				<string>UCL</string>
    				<key>id</key>
    				<string>501</string>
    			</dict>
    		</array>
    	</dict>
    </array>
</plist>

Nasıl okunur bu dosya yok. Çeşitli yöntemler denedim ama hiç biri işe yaramadı. Bile dosya için doğru yerde arıyorum bilmiyorum. Başvuru için aşağıdaki yöntemlerden çalışıyorum:

NSString* errorDesc = nil;
NSPropertyListFormat format;
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
NSData* plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
contentArray = (NSArray*)[NSPropertyListSerialization
    								 propertyListFromData:plistXML
    								 mutabilityOption:NSPropertyListMutableContainersAndLeaves
    								 format:&format
    								 errorDescription:&errorDesc];

if (!contentArray) {
    NSLog(errorDesc);
    [errorDesc release];
}

ya

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];

ya

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"leagues.plist"];
NSLog(fooPath);
contentArray = [NSArray arrayWithContentsOfFile:fooPath];
NSLog(@"%@",contentArray);

Bu sonunda beni tamamen deli ediyor. Lütfen yardım edin!

Teşekkür ederim

CEVAP
15 NİSAN 2009, ÇARŞAMBA


NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentArray = [NSDictionary dictionaryWithContentsOfFile:plistPath];

Bu cevap doğru dosya uygulaması olduğuna emin misin? Projenize eklemek ve eğer app bundle içine kopyalanmış oluyor olmadığını görmek için kontrol ettiniz mi? Değilse, olabilir dosya özellikle eğer birden çok hedef varsa, kolayca düşülebilecek bir hata inşa ediyoruz hedef eklenmedi.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • KendrickLamarVEVO

    KendrickLama

    9 ŞUBAT 2011
  • PhoneArena

    PhoneArena

    7 NİSAN 2006
  • SRC RECORDS

    SRC RECORDS

    2 EKİM 2006