[4주 - 3일차] 학습 정리

okstring·2020년 11월 26일
0

codesquad-cocoa

목록 보기
18/25

Dictionary in Array 에서 Value 찾아 삭제

if let index = csvData.index(where: { $0["email"] as! String == email }) {
    csvData.remove(at: index)
    return true

FileManager

// 바탕화면에 파일 있는지 확인하고 csv파일 생성
let fileManager = FileManager.default
let desktopPath = "/Users/maylily/Desktop"
let desktopPathURL = URL.init(fileURLWithPath: "/Users/maylily/Desktop")

do {
    // Add Component
    let csvPath = desktopPathURL.appendingPathComponent("\(file).csv")
    // isExists
    if fileManager.fileExists(atPath: desktopPath + "/\(file).csv") { return false }
    let text = self.rawData

    try text.write(to: csvPath, atomically: false, encoding: .utf8)
    return true

} catch let error as NSError {
    print("Error : \(error.localizedDescription)")
    return false
}


// 디렉토리 생성
var documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let dataPath = documentsDirectory.appendingPathComponent("FileManager Directory")
do {
    // 디렉토리 생성
    try fileManager.createDirectory(atPath: dataPath.path, withIntermediateDirectories: false, attributes: nil)

} catch let error as NSError {
    print("Error creating directory: \(error.localizedDescription)")
}


//urls(for:in:) 메소드를 통해 특정 경로에 접근한다.
//해당 경로에 추가 경로를 지정하는 방식으로 디렉토리 명을 추가한다.
//디렉토리를 생성한다.

reference

String.write

let str = "Super long string here"
let filename = getDocumentsDirectory().appendingPathComponent("output.txt")

do {
    try str.write(to: filename, atomically: true, encoding: String.Encoding.utf8)
} catch {
    // failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding
}
func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
}

reference

Get Millisecond

let timestamp = NSDate().timeIntervalSince1970

싱글턴(Singleton)

싱글턴은 '특정 클래스의 인스턴스가 오직 하나임을 보장하는 객체'를 의미
reference

RunLoop.main.run()

이벤트 처리를 위한 loop로 입력소스와 타이머소스를 주로 처리.

reference

reference2

profile
step by step

0개의 댓글

관련 채용 정보