μ»΄λ°μΈ νμ΅μ€ μμ μ½λμμ κ½€λ μ μ©ν νλ‘ν μ½μ μ νκ²λμλ€. λ°λ‘ CustomStringConvertible νλ‘ν μ½μ΄λ€. μ΄ νλ‘ν μ½μ μ±νν κ°μ²΄λ description νλ‘νΌν°λ₯Ό νμμ μΌλ‘ κ°μ§κ²λμ΄ μμ μ΄ μ΄λ€κ°μ²΄μΈμ§ μ€λͺ ν μ μκ²λλ€. κ±°λμ λ―Ένκ³ μν©λ³ μμ λ₯Ό ν΅ν΄ μμ보μ
1. λλ²κΉ λ° λ‘κΉ
struct NetworkRequest: CustomStringConvertible {
var url: String
var method: String
var headers: [String: String]
var description: String {
"Request to \(url) with method \(method) and headers \(headers)"
}
}
let request = NetworkRequest(url: "https://api.example.com",
method: "GET",
headers: ["Authorization": "Bearer token"])
print("Sending network request: \(request)")
// Sending network request: Request to https://api.example.com with method GET and headers ["Authorization": "Bearer token"]
2. μ¬μ©μ μΈν°νμ΄μ€ μμ νμ
struct Event: CustomStringConvertible {
var title: String
var date: Date
var description: String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
return "\(title) on \(dateFormatter.string(from: date))"
}
}
let event = Event(title: "Swift Conference", date: Date())
label.text = event.description
//Swift Conference on May 13, 2024
3. API μλ΅ ν¬λ§·ν
struct ApiResponse: CustomStringConvertible {
var status: Int
var messafe: String
var description: String {
"Response with status \(status): \(message)"
}
}
let response = ApiResponse(status: 200, message: "OK")
print("API Response: \(response)")
// API Response: Response with status 200: OK
4. ν μ€νΈ λ° λ¬Έμν
// Unit Test example
func testEventDescription() {
let event = Event(title: "Swift Meetup", date: Date())
XCTAssertEqual(event.description, "Swift Meetup on \(Date())")
}
// Test Passed: 'Swift Meetup on May 13, 2024' equals 'Swift Meetup on May 13, 2024'
'π¦ Flutter' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| Combine | 8. Sequencing Operators (0) | 2024.05.13 |
---|---|
| Combine | 7. Scheduling Operators (0) | 2024.05.13 |
| Combine | 6. Timing Operators (0) | 2024.05.12 |
| Combine | 5. Combining Operators (0) | 2024.05.11 |
Flutter Widgetμ μλμ리 (0) | 2024.05.10 |