2016-05-12 91 views
-1

我正在用我的設備創建一個帶有theos的JB tweak。如何讀取plist字典,並將鍵(不是值)保存在另一個plist的數組字符串中?

我想讀的plist字典,如:

ASNPrefs.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"> 

     <dict> 
      <key>ASNcenterEnabled</key> 
      <false/> 
      <key>ASNcornerEnabled</key> 
      <false/> 
      <key>ASNnoCenterEnabled</key> 
      <false/> 
      <key>ASNdepthSizeEnabled</key> 
      <false/> 
     </dict> 
     </plist> 

和只寫密鑰作爲數組到像其他的plist:

ASNkeys.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"> 

     <dict> 
      <key>ASNkeys</key> 
      <array> 
       <string>ASNcenterEnabled</string> 
       <string>ASNcornerEnabled</string> 
       <string>ASNnoCenterEnabled</string> 
       <string>ASNdepthSizeEnabled</string> 
      </array> 
     </dict> 
    </plist> 

我創建了一個按鈕,這個動作,但沒有任何反應:

- DEFINED AT BEGINNING OF .MM: 


#define prefs @"/User/Library/Preferences/ASNPrefs.plist" 
#define asnKeysPath @"/User/Documents/asnKeys.plist" 
#define asnKeysDict [[NSDictionary dictionaryWithContentsOfFile:asnKeysPath] objectForKey:@"keys"] 



- IN THE IMPLEMENTATION: 


    -(void)ASNkeys { 
     NSDictionary *asnPrefs = [NSDictionary dictionaryWithContentsOfFile:prefs]; 
     NSArray *allKeys = [prefs allKeys]; 

    [allKeys writeToFile:asnKeysDict atomically:YES]; 
     } 

謝謝!

+0

我不知道如何處理這個......建議?謝謝! – Olivier

+0

好的!對不起......片刻.. – Olivier

回答

0

終於!!

經過很多改變...謝謝你這麼馬克CRD!

NSString *arrayPath; 
NSString *dictPath; 
NSString *origPath; 

// Get path to documents directory 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

      if ([paths count] > 0) { 

       origPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"com.maat.asnPrefs.plist"]; 

       NSDictionary *dictOrig = [NSDictionary dictionaryWithContentsOfFile:origPath]; 

       NSArray *ASNKeys = [dictOrig allKeys]; //ForObject:@"true"]; 

       NSDictionary *dictionary = @{@"ASNKeys" : allKeys}; 

       // Path to save array data 
       arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"]; 

       // Path to save dictionary 
       dictPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"asnKeys.plist"]; 

       // Write array 
       [asnKeys writeToFile:arrayPath atomically:YES]; 

       // Write dictionary 
       [dictionary writeToFile:dictPath atomically:YES]; 
} 
0

由於您的plist是字典使用dictionaryWithContentsOfFile:NSDictionary(而不是從arrayWithContentsOfFile:NSArray)。一旦你有字典,你可以使用allKeys方法獲得一個鍵的數組,然後你可以將該數組寫入plist。

HTH

+0

我會稍後再試...現在我要把我的孩子留在學校...謝謝你的回答! – Olivier

+0

我昨天嘗試了很多次......我無法弄清楚如何解決它。我用新代碼編輯帖子... – Olivier

+0

我沒有注意到,在編輯之前,您正在嘗試寫入您的應用程序包,這是不允許的。編輯完成後,您將文件路徑傳遞給'pathForResource',這是不正確的。您應該閱讀蘋果的[文件系統基礎](https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html),瞭解iOS應用程序可以存儲文件的位置。你是否在使用調試器,遍歷代碼並檢查每個變量的值? HTH – CRD