I suppose I'd like to be able to find out for any storage, not just the system disk, but that's most important.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
- Converting (u)int64_t to NSNumbers
EDIT fileSystemAttributesAtPath: is deprecated, use attributesOfFileSystemForPath:error: as NSD suggested. I made a mistake when I thought it didn't work.
I tried this, attributesOfItemAtPath:error but the dict returned didn't seem to have the NSFileSystemFreeNodes key.
After looking around a bit, it seems like fileSystemAttributesAtPath: is the method that returns it. Weird.
Get from this post:
The main difference is that you should use NSSearchPathForDirectioriesInDomains, otherwise I was getting correct value on simulator, but on the device '/' folder reference returns me something like 190MB which was not right.
Swift 3.0-4.0 on MacOS
I've done this for macOS and haven't had an issue. The output is in bytes so you have to divide it by the 1024.
The
FileManager.default.attributesOfFileSystem(forPath: "/")
is just getting the various attributes available for the root directory's file system. One of those attributes happens to be the Free Disk Space.You access that key (key:value) and typecast it to an Int to get your free disk space.
Hard disk device manufacturers, use:
1GB = 1000MB
1MB = 1000 KB etc.
If you see a 8GB USB stick in Windows always shows less space than real (like 7,8 GB) because it is considered 1 GB = 1024 MB). In OSX the same USB stick is 8GB (real).
so
(freeSpace / 1073741824)
must be(freeSpace / 1000000000)
at least in OSX
Use
-[NSFileManager attributesOfFileSystemForPath:error:]
I use this: