1. 程式人生 > >Mac 下命令列使用Cmake與PlistCpp開源庫的使用

Mac 下命令列使用Cmake與PlistCpp開源庫的使用

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Plist::dictionary_type dict;
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"binaryExample1" ofType:@"plist"];
    const char* path = [plistPath UTF8String];
    Plist::readPlist(path, dict);
    for(Plist::dictionary_type::const_iterator it = dict.begin(); it != dict.end(); ++it)
    {
        cout<< "key: "<< it->first <<endl;
    }
    
    //讀陣列
    const Plist::array_type& plistArray = boost::any_cast<const Plist::array_type&>(dict.find("testArray")->second);
    cout << boost::any_cast<const int64_t&>(plistArray[0]) << endl;
    cout << boost::any_cast<const string&>(plistArray[1]).c_str() << endl;
    
    //讀字典
    const Plist::dictionary_type& plistDic = boost::any_cast<const Plist::dictionary_type&>(dict.find("testDict")->second);
    Plist::dictionary_type::const_iterator it = plistDic.begin();
    cout << "key:" << it->first << endl;
    cout << boost::any_cast<const string&>(it->second) << endl;
    
    //讀一個變數
    const Plist::Date& date = boost::any_cast<const Plist::Date&>(dict.find("testDate")->second);
    std::string strDate = date.timeAsXMLConvention();
    cout << boost::any_cast<const string&>(strDate) << endl;
    
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path1 = paths[0];
    NSString *filePath = [path1 stringByAppendingPathComponent:@"writeBinary.plist"];
    const char* filePath1 = [filePath UTF8String];
    
    //寫陣列1
    Plist::dictionary_type dic;
    
    dic.insert(make_pair("testArray1", plistArray));
    //Plist::writePlistBinary(filePath1, dic);
    
    //寫陣列2
    vector<boost::any> array(2);
    array[0] = 50;
    array[1] = string("mystring ok in array");
    cout << boost::any_cast<const int&>(array[0]) << endl;
    cout << boost::any_cast<const string&>(array[1]).c_str() << endl;
    dic["testArray2"] = array;
    Plist::writePlistBinary(filePath1, dic);
    
    Plist::dictionary_type dicR;
    Plist::readPlist(filePath1, dicR);
    //讀陣列
    const Plist::array_type& plistArrayR = boost::any_cast<const Plist::array_type&>(dicR.find("testArray2")->second);
    cout << boost::any_cast<const int64_t&>(plistArrayR[0]) << endl;
    cout << boost::any_cast<const string&>(plistArrayR[1]).c_str() << endl;
}