본문 바로가기

프로그래밍

(356)
[Swift] publishing changes from background threads is not allowed; make sure to publish values from the main thread swiftui 에서 async 콘텍스트에서 UI 와 관련된 변수를 업데이트할 때 발생한다. xcode에서 보라색으로 나타나는 워닝이다. UI 관련 변수를 업데이트할 때에는 main 쓰레드에서 업데이트 될 수 있도록, await MainActor.run { } 블록으로 감싸주어야 한다. 이 방법은 이전에 사용하던 DispatchQueue.main 을 대체하는 방식이다. 이 방법도 가능하지만, 더 아름다운 방식은, @MainActor 어노테이션을 사용하는 것. 해당 변수를 업데이트하는 코드가 들어있는 함수 선언에 어노테이션을 더해준다. @MainActor func uiUpdateFunc() - ref : Modern Concurrency in Swift Chapter 1, 2
[Python초보] ValueError: invalid literal for int() with base 10: '' invalid literal for int() with base 10: '' 10진수 int() 로 변환할 수 없는 문자열: '' 이라는 뜻입니다. 한번 이 에러를 만들어 봅시다. >>> num = int("1000") >>> num 1000 "1000" 이라는 문자열을 숫자형으로 변환하여 num 이란 변수에 넣는 코드입니다. "1000"이라는 문자열은 정수를 나타내는 문자열이기 때문에, 문제없이 변환됩니다. >>> s = "abc" >>> num = int(s) Traceback (most recent call last): File "", line 1, in num = int(s) ValueError: invalid literal for int() with base 10: 'abc' "abc" 라는 문..
[SWIFT] dump() 함수 출력을 문자열로 바꾸는 방법 dump 함수는 클래스 인스턴스의 내부 속성까지 간단히 출력해 주기 때문에 디버깅시에 편리하다. 그런데, 출력 내용을 문자열로 저장하여, stdout, stderr 가 아닌 곳으로도 출력/저장하고 싶었다. 스택오버플로우에 간단한 답변이 있었다. https://stackoverflow.com/a/42094841/100093 let myClass = MyClass() var myClassDumped = String() dump(myClass, to: &myClassDumped) https://github.com/apple/swift/blob/master/stdlib/public/core/OutputStream.swift 위 링크를 인용하고 있는데, 직접 영문설명을 읽어보는 게 좋겠지만, 요약하면, print..
[Python] 십진수소수 이진수로 변환하기 십진수 정수를 이진수로 변환하는 것은 파이썬이 기본으로 제공하는 bin 함수를 사용하면 된다. 소수점 이하 자릿수를 포함한 십진수를 이진수로 표현하는 함수를 만들어 보았다. 입력을 float 으로 변환하면, 부동소수점 오류로 정확한 계산이 불가능하다. 분수를 다룰 수 있는 fractions 모듈을 이용하여 정확하게 소수점 아래까지 구할 수 있다. 순환마디까지 구할 수 있는데, 일단 무시하고 30자리까지 구하는 걸 구현했다. from fractions import Fraction def conv2bin(s): x = Fraction(s) x1 = x//1 x2 = x - x1 digits = [] tail = "..." for _ in range(30): if x2 == 0: tail = "" break ..
[XPC] 새로운 타겟에서 xpc service 사용하기 Error Domain=NSCocoaErrorDomain Code=4099 xpc service 를 만들었다. xpc service 를 사용해야 하는 어플리케이션 타겟을 만들어, 기존에 만든 xpc service 의 함수를 호출하려 했다. 그런데, 함수호출을 했는데도, 서비스와의 연결이 실패했다는 에러가 발생하였다. Received error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.xxxx.xxxxxpc was invalidated: failed at lookup with error 3 - No such process." UserInfo={NSDebugDescription=The connection to service named com.xxxx.xxxxxpc was inval..
communcation over XPC is asynchronous https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html Designing an Interface The NSXPCConnection API takes advantage of Objective-C protocols to define the programmatic interface between the calling application and the service. Any instance method that you want to call from the opposite side of a connection must be explicitl..
[Swift|번역] Process waitUntilExit 은 비동기 completion handler 와 같이 쓰지 않는다. https://stackoverflow.com/a/49541564 동기함수인 waitUntilExit() 은 비동기적 completion handler 와는 같이쓰기 어렵다. 비동기 completion handler 를 사용한다면, exit 을 기다리는 건 의미없는 일이다. --- pipe의 fileHandleForReading 을 클로져에서 사용하기 때문에 double free 에러가 발생하는 것일 수 있다. stdout 을 동기적으로 읽으라.
Flutter Warning: Operand of null-aware operation '!' hastype 'SchedulerBinding' which excludes null. 올해 초에 만들었던 플러터 프로젝트를 다시 빌드하다 보니 다음과 같은 경고메시지가 떴다. 동작은 했지만, 우찌 없앨지 고민했다. /D:/DEV_FLUTTER/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_math_fork-0.5.0/lib/src/widgets/selectable.dart:459:24: Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null. - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart' ('/D:/DEV_FLUTTER/flutter/packages/..