본문 바로가기
728x90

iOS/swift 6

숫자 다루기 나눈 수의 소수점 사용하는 숫자의 타입이 Int일 경우 결과값도 Int형이므로 사용하는 숫자의 타입을 Double로 바꾸어야 한다. let num1 = 3 let num2 = 2 print(num1 / num2) // 1 let num1: Double = 3 let num2: Double = 2 print(num1 / num2) // 1.5 제곱근 찾기 let number = 121 print(sqrt(number)) // 11 2023. 11. 7.
attributedString 텍스트 양쪽에 이미지 넣기 let attributedString = NSMutableAttributedString(string: "") let leftImageAttachment = NSTextAttachment() leftImageAttachment.image = UIImage(named: "quotes_left") attributedString.append(NSAttributedString(attachment: leftImageAttachment)) attributedString.append(NSAttributedString(string: " \(msg!) ")) let rightImageAttachment = NSTextAttachment() rightImageAttachment.image = UI.. 2022. 12. 26.
Array append(_:) vs append(contentsOf:) append : 하나의 element를 배열 맨 뒤에 추가해준다 var numbers = [1, 2, 3, 4, 5] numbers.append(100) print(numbers) // Prints "[1, 2, 3, 4, 5, 100]" append(contentsOf): 여러개의 elements를 배열 맨 뒤에 추가 해준다. var numbers = [1, 2, 3, 4, 5] numbers.append(contentsOf: 10...15) print(numbers) // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]" 출처 : https://developer.apple.com/documentation.. 2022. 12. 21.
String 특정 문자 제거 양끝 문자 제거 중간에 있는 문자는 제거 불가능 var helloStr = "Hello!" var iosStr = "#iOS" var swiftStr = "#Swift!" helloStr.trimmingCharacters(in: ["!"])//Hello iosStr.trimmingCharacters(in: ["#"])//iOS swiftStr.trimmingCharacters(in: ["#","!"])//Swift 중간에 있는 문자 제거 var str = "Hello~!@@@, Swift Zedd" str.components(separatedBy: ["~","!","@",",","Swift"])//error!! var str = "Hello~!@@@, Zedd" str.components.. 2022. 9. 22.
진법 변환 Radix https://developer.apple.com/documentation/swift/int/init(_:radix:) 문자열이나 기수에서 integer 값을 만들어 준다. 10진수 -> 2진수 let num = 78 print(String(num, radix: 2)) // 1001110 2진수 -> 10진수 let num = "1001110" print(Int(num, radix: 2)!) // 78 nonzeroBitCount https://developer.apple.com/documentation/swift/fixedwidthinteger/nonzerobitcount 이진값에서 1의 개수를 알려준다. let num = 78 print(num.nonzeroBitCount) // 4 2022. 8. 31.
Alamofire timeout set(Swift5) 참고: https://alamofire.github.io/Alamofire/Classes/SessionDelegate.html https://moonggi-dev-story.tistory.com/7 Alamofire 라이브러리에 timeout 을 달아보자 iOS 대표 라이브러리라 불리는 Alamofire 에 대해 timeout 셋팅을 하고싶었다.. 테스트 결과 기본적으로 1분정도 timeout이 셋팅 되어 있는거 같다. 현재 ARS 같은경우 1분을 훌쩍넘는 시간동안 인증을 한 moonggi-dev-story.tistory.com sessionManager 기본적으로 제공되는 alamofire의 sessionManager의 속성을 커스텀하고 싶을 때 configuration을 이용한다. 네크워크 통신 중 .. 2022. 8. 25.