Error: Ambiguous reference to the "index" of the participant in Swift 3

I downloaded the beta version of Xcode 8 and converted my syntax to Swift 3. When I did this, I got the error of the same name with this code (this had not happened before):

Swift 3:

do {
    let fileAttributes = try FileManager.default().attributesOfItem(atPath: fileURL.path!) // Error here
    let fileSizeNumber = fileAttributes[NSFileSize] as! NSNumber
    fileSize = fileSizeNumber.longLongValue
} catch _ as NSError {
    print("Filesize reading failed")
}

Swift 2:

do {
    let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(fileURL.path!)
    let fileSizeNumber = fileAttributes[NSFileSize] as! NSNumber
    fileSize = fileSizeNumber.longLongValue
} catch _ as NSError {
    print("Filesize reading failed")
}

Is this a bug in Swift 3, or am I missing something?

I know that there are many questions about the same error, but this does not fix my problem. I am happy to make changes for clarification.

Thanks in advance!

+4
source share
3 answers

I think something like this should work:

do {
    let fileAttributes = try FileManager.default().attributesOfItem(atPath: file.path!)
    if let fileSizeNumber = fileAttributes["NSFileSize"] as? NSNumber {
        let fileSize = fileSizeNumber.int64Value

    }
} catch let error as NSError {
    print("Filesize reading failed: \(error.debugDescription)")
}

NSFileSize , . , , , ( , , ).

Xcode 8 GM:

FileAttributeKey.size , ( @rudy ). :

do {
    let attributes = try FileManager.default.attributesOfItem(atPath: file.path)
    if let size = attributes[FileAttributeKey.size] as? NSNumber {
        let fileSize = size.int64Value
        print(fileSize)
    }
} catch {
    print(error.localizedDescription)
}
+7

Swift 3:

var fileSize: UInt64 // size in bytes

do {
    let fileAttributes: NSDictionary? = try FileManager.default().attributesOfItem(atPath: fileURL.path!)
    if let fileSizeNumber = fileAttributes?.fileSize() { fileSize = fileSizeNumber }
} catch let error as NSError {
    print("Filesize reading failed: \(error.debugDescription)")
}

NSDictionary, , .fileSize():

  • .fileGroupOwnerAccountName()
  • .fileModificationDate()
  • .fileOwnerAccountName()
  • .filePosixPermissions()
  • .fileSize()
  • .fileSystemFileNumber()
  • .fileSystemNumber()
  • .fileType()

, -Click Swift.

+1

, :

fileAttributes[FileAttributeKey.size.rawValue]

(Swift 3 XCode8 WWDC)

+1
source

All Articles