ไม่สามารถแปลงค่าเวลา 'Int' เป็นประเภทอาร์กิวเมนต์ที่คาดหวัง 'NSPropertyListReadOptions' ใน Swift 2

ฉันกำลังอัปเกรดโค้ดที่มีอยู่เป็น Swift 2 และต้องการความช่วยเหลือในการบันทึกไฟล์ Plist

รหัสนี้ทำงานใน Xcode 6.3 แต่ตอนนี้ด้วย Xcode 7 และ Swift 2 มันแสดงข้อผิดพลาดนี้ให้ฉัน:

ไม่สามารถแปลงค่าของเวลา 'Int' เป็นประเภทอาร์กิวเมนต์ที่คาดหวัง 'NSPropertyListReadOptions' (หรือที่เรียกว่า 'NSPropertyListMutabilityOptions')

var resultValue = "Value goes here"


@IBAction func saveNote(sender: AnyObject) {
    // Save note to plist
    var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    var pathForThePlistFile = appDelegate.plistPathInDocument

    // Extract the content of the file as NSData
    var data:NSData =  NSFileManager.defaultManager().contentsAtPath(pathForThePlistFile)!
    // Convert the NSData to mutable array
    var notesArray = (try! NSPropertyListSerialization.propertyListWithData(data, options: Int(NSPropertyListMutabilityOptions.MutableContainersAndLeaves.rawValue), format: nil)) as! NSMutableArray
    //

    notesArray.addObject(resultValue)
    // Save to plist
    notesArray.writeToFile(pathForThePlistFile, atomically: true)
}

กรุณาช่วย!


person AJ152    schedule 04.09.2015    source แหล่งที่มา


คำตอบ (2)


ฉันไม่คิดว่าคุณจำเป็นต้องแปลงพารามิเตอร์ options เป็น Int แต่ควรใช้ค่าประเภท NSPropertyListMutabilityOptions:

var notesArray = (try! NSPropertyListSerialization.propertyListWithData(data, options:NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil)) as! NSMutableArray
person Peter Willsey    schedule 04.09.2015

คุณไม่สามารถกำหนดตัวเลือกของตัวเองโดยการพัฒนาแอปเปิ้ลของคุณเองได้ มีกฎบางอย่างและแจ้งไปแล้วว่าคุณสามารถใช้คุณสมบัติตัวเลือกได้เพียง 2 รายการเท่านั้น เพื่อความเข้าใจของคุณ ฉันกำลังเชื่อมโยงเอกสารของ NSropertyListSerialization โปรดอ่านอย่างละเอียด แล้วคุณจะเข้าใจวิธีดำเนินการดังกล่าว

ไม่ว่าคุณจะใช้

NSPropertyListMutabilityOptions

ทั้ง

NSPropertyListReadOptions

หรือไม่มีเลย

int จะไม่รองรับที่นี่

person AFTAB MUHAMMED KHAN    schedule 04.09.2015