Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 读取值。plist swift_Ios_Swift_Random_Property List - Fatal编程技术网

Ios 读取值。plist swift

Ios 读取值。plist swift,ios,swift,random,property-list,Ios,Swift,Random,Property List,我在斯威夫特有一个.plist。其设置如下所示 我很难找到有关如何从.Plist读取信息的信息。 我希望能够在表情列表中随机选择845项中的一项。一旦我有了那个项目,然后我想访问那个项目的表情字符串值和它的描述字符串值 从程序上讲,我如何访问项目列表中的随机项目?然后才能访问该特定项的属性?首先将plist加载到字典数组中。每本字典代表一个表情符号。然后生成数组的随机索引,并在该索引处拉出字典。现在使用字典访问表情符号的属性 var emojiArray: NSArray? if let pat

我在斯威夫特有一个.plist。其设置如下所示

我很难找到有关如何从.Plist读取信息的信息。 我希望能够在表情列表中随机选择845项中的一项。一旦我有了那个项目,然后我想访问那个项目的表情字符串值和它的描述字符串值


从程序上讲,我如何访问项目列表中的随机项目?然后才能访问该特定项的属性?

首先将plist加载到字典数组中。每本字典代表一个表情符号。然后生成数组的随机索引,并在该索引处拉出字典。现在使用字典访问表情符号的属性

var emojiArray: NSArray?
if let path = NSBundle.mainBundle().pathForResource("name-of-file", ofType: "plist"){
    emojiArray = NSArray(contentsOfFile: path)
}

if let array = emojiArray {
    let randomIndex = arc4random_uniform(array.count) // random number from 0 to array.count - 1
    let emojiDictionary = array[randomIndex]
    println("emoji value: \(emojiDictionary["emoji"]), emoji description: \(emojiDictionary["description"])"
}

如果让emojiArray=emojiArray{…}@dash,我在使用未解析标识符“emoji”和“description”时出错,因为字典键是字符串,所以需要引号,对此表示抱歉!更新代码。