File Manager Example With Swift

FileManagerExample

FileManager를 쉽게 설명하기 위한 문서입니다.

    let fileManager = FileManager.default
    let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]

보통 위의 코드처럼 FileManager를 사용할 때, 싱글턴 객체를 이용하여 urls를 통해 for, in의 매개변수들을 사용합니다.
for은 SearchPathDirectory 으로 값을 받고 있으며, in은 SearchPathDomainMask으로 받고있습니다.

####요약하면 for : 폴더 위치, in : 접근제어 라고 보시면 됩니다.

##SearchPathDomainMask
-먼저 FileManager의 위치 권한 SearchPathDomainMask 를 보면 다음과 같이 나뉘어져 있다.

    public struct SearchPathDomainMask : OptionSet {

        public init(rawValue: UInt)

        
        public static var userDomainMask: FileManager.SearchPathDomainMask { get } // user's home directory --- place to install user's personal items (~)

        public static var localDomainMask: FileManager.SearchPathDomainMask { get } // local to the current machine --- place to install items available to everyone on this machine (/Library)

        public static var networkDomainMask: FileManager.SearchPathDomainMask { get } // publically available location in the local area network --- place to install items available on the network (/Network)

        public static var systemDomainMask: FileManager.SearchPathDomainMask { get } // provided by Apple, unmodifiable (/System)

        public static var allDomainsMask: FileManager.SearchPathDomainMask { get } // all domains: all of the above and future items
    }

##Simulator 실행 결과

    let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    //결과값
    //file:///Users/janghyowon/Library/Developer/CoreSimulator/Devices/6F684BD3-AD68-416F-98A2-DF6333813AFD/data/Containers/Data/Application/1C8689AA-6EF7-44A2-B85A-B2E138EDB7D2/Documents/
    
    let documentsURL = fileManager.urls(for: .documentDirectory, in: .allDomainsMask)[0]
    //결과값
    //file:///Users/janghyowon/Library/Developer/CoreSimulator/Devices/6F684BD3-AD68-416F-98A2-DF6333813AFD/data/Containers/Data/Application/B177AA48-D9BB-4921-B264-82230C49722F/Documents/

##Simulator 실행 불가값

  • localDomainMask
  • networkDomainMask
  • systemDomainMask

GitHub

View Github