convert NSData Length from bytes to megs

前端 未结 3 2095
抹茶落季
抹茶落季 2021-02-03 19:20

I am trying to NSLog the number of megs my NSData object is however currently all I can get is bytes by using

NSLog(@\"%u\", myData.le         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-03 19:53

    With Swift 5.1 and iOS 13, you can use one of the 5 following ways to solve your problem.


    #1. Using ByteCountFormatter's string(fromByteCount:countStyle:) class method

    The following sample code shows how to implement string(fromByteCount:countStyle:) in order to print a file size by automatically converting bytes to a more appropriate storage unit (e.g. megabytes):

    import Foundation
    
    let url = Bundle.main.url(forResource: "image", withExtension: "png")!
    let data = try! Data(contentsOf: url)
    
    let byteCount = data.count
    print(byteCount) // prints: 2636725
    
    let displaySize = ByteCountFormatter.string(fromByteCount: Int64(byteCount), countStyle: .file)
    print(displaySize) // prints: 2.6 MB
    

    #2. Using ByteCountFormatter's string(fromByteCount:) method

    The following sample code shows how to implement ByteCountFormatter's string(fromByteCount:) in order to print a file size by manually converting bytes to megabytes:

    import Foundation
    
    let url = Bundle.main.url(forResource: "image", withExtension: "png")!
    let data = try! Data(contentsOf: url)
    
    let byteCount = data.count
    print(byteCount) // prints: 2636725
    
    let formatter = ByteCountFormatter()
    formatter.allowedUnits = [.useMB]
    formatter.countStyle = .file
    let displaySize = formatter.string(fromByteCount: Int64(byteCount))
    print(displaySize) // prints: 2.6 MB
    

    #3. Using ByteCountFormatter's string(from:countStyle:) class method and Measurement

    The following sample code shows how to implement string(from:countStyle:) in order to print a file size by automatically converting bytes to a more appropriate storage unit (e.g. megabytes):

    import Foundation
    
    let url = Bundle.main.url(forResource: "image", withExtension: "png")!
    let data = try! Data(contentsOf: url)
    
    let byteCount = data.count
    print(byteCount) // prints: 2636725
    
    let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
    let displaySize = ByteCountFormatter.string(from: byteSize, countStyle: .file)
    print(displaySize) // prints: 2.6 MB
    

    #4. Using ByteCountFormatter's string(from:) method and Measurement

    The following sample code shows how to implement ByteCountFormatter's string(from:) and Measurement in order to print a file size by manually converting bytes to megabytes:

    import Foundation
    
    let url = Bundle.main.url(forResource: "image", withExtension: "png")!
    let data = try! Data(contentsOf: url)
    
    let byteCount = data.count
    print(byteCount) // prints: 2636725
    
    let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
    let formatter = ByteCountFormatter()
    formatter.allowedUnits = [.useMB]
    formatter.countStyle = .file
    let displaySize = formatter.string(from: byteSize)
    print(displaySize) // prints: 2.6 MB
    

    #5. Using MeasurementFormatter's string(from:) method and Measurement

    The following sample code shows how to implement Measurement and MeasurementFormatter's string(from:) in order to print a file size by manually converting bytes to megabytes:

    import Foundation
    
    let url = Bundle.main.url(forResource: "image", withExtension: "png")!
    let data = try! Data(contentsOf: url)
    
    let byteCount = data.count
    print(byteCount) // prints: 2636725
    
    let byteSize = Measurement(value: Double(byteCount), unit: UnitInformationStorage.bytes)
    let convertedSize = byteSize.converted(to: .megabytes)
    let formatter = MeasurementFormatter()
    let displaySize = formatter.string(from: convertedSize)
    print(displaySize) // prints: 2.637 MB
    

提交回复
热议问题