기본 콘텐츠로 건너뛰기

iOS Swift 학습 로그

오래간만에 의뢰로 iOS app을 만들고 있다.
정말 많이 편해졌다. 놀랍다.
swift만해도 벌써 몇번이나 업데이트 하고;

일단 pod 파일. cocoapod은 정말 너무 좋다.
먼저 프로젝트 생성 하고 Meteor DDP를 쓰기 위해 touch Podfile 하고

platform :ios, '8.0'
use_frameworks!

pod 'Meteor'

이렇게 쓰면 끝
pod install 한 뒤 *.xcworkspace 를 열면 끝.
* 주의: install중 중간에 정지하면 기존 pod 가 망가질 수 있음

사용은 AppDelegate.swift를 열고
전역 변수로 
let Meteor = METCoreDataDDPClient(serverURL: NSURL(string: "ws://www.YOURMETEORAPP.com/websocket")!)

지정하고 

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        Meteor.connect()
        
        return true
    }

이런 식으로 Application 기동 시 Meteor 접속.

* swiftDDP 를 내장한 RealmMeteor가 더 좋아보인다.

Storyboard 의 segue (세그웨이라고 읽음) 상당히 좋았다.

일단 첨에 진입점이 되는 Controller 에 Show Attributes Inspector를 열고 

Is Initial View Controller를 체크.
실수로 지우거나 새로 Tab Bar/Split View/Collection View/Navigator Controller 등을 생성할 때 꼭 확인하자.

View와 연결되는 *.swift controller들을 만들 땐import Foundation

import UIKit

class YOURController:UIViewController {
}

으로 시작.

간단한 사용자 정보 저장은 Key/Value Store인 NSUserDefaults.standardUserDefaults() 를 사용.

읽기는 (Boolean의 경우)

userDefaults.boolForKey("KEY")

쓰기는

userDefaults.setBool(true, forKey: "KEY")

이런 식. 다른 타입도 대동소이.

Contstraint는 뭔가 헷갈린다.
배치를 다 한 뒤 해주는 게 삽집을 덜 하는 듯.

Segue는 swift안에서 수동으로 할 때 Identifier 를 지정해야 한다.



이렇게 Segue를 선택(파랑색으로 변함)하고 Attribute Inspector에서 Identifier를 지정.

performSegueWithIdentifier("noticeSegue", sender: self)
이런 식으로 Identifier를 넣어서 사용한다.
modal Popup의 경우 viewDidLoad() 에서 호출해도 잘 되더라.

닫을 때는
self.dismissViewControllerAnimated(true, completion: {})

이런 식으로 닫으면 됨.

스토리보드와 코드사이에서 ctrl 드래그로 IBOutlet 이나 IBAction을 지정할 때
오른쪽 상단에 원두개가 겹쳐있는 아이콘(Show Assistant editor)를 열면 편리.

주의할 점은 왼쪽에 있는 Project Navigator 에서 파일을 선택해도 왼쪽 에디터만 바뀐다.
오른쪽은 상단 바를 클릭해서 찾아가야함.

UIWebView 사용 시 info.plist 에 권한을 열어줘야함.
안그러면 사용할 수 없다.

App Transport Security Settings > Allow Arbitrary Loads 에서 Boolean 을 YES로

초기 로딩할 땐
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://YOURURL/")!)) 

이런 식으로 쓰면 된다.
Memory Warning이 나면서 Crash 할 수 있는데

    override func didReceiveMemoryWarning() {
        // prevent MemoryWarning
        NSURLCache.sharedURLCache().removeAllCachedResponses()
    }

didReceiveMemoryWarning를 받아서 캐쉬를 지워주면 좀 낫다고 한다.

UIImagePickerController 사용 시 해당 Controller 에 UIImagePickerControllerDelegate, UINavigationControllerDelegate 를 추가해야함.

class CameraController:UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  ...
}

이런 식.

objective-c 형식의 파일은 header(*.h)가 들어간 파일을 swift로 bridge 파일 만들고 Build Settings 에 Swift Compiler - Code Generation > Objective-C Bridging Header 아래 Debug / Release 아래 각각 만들어 준다.


마지막으로 *.xcdatamodel 에서 Attribute명으로 description 을 사용할 수 없다. 주의.

댓글

이 블로그의 인기 게시물

세상 간단한 https(+secured websocket): Caddy

nginx, apache2 같은 걸로 매번 certbot 연동을 통해 https 하는게 지겨워서 알아보니 Caddy라는게 있더라. 설치법은 강하게 크기 위해 알아서 해본다. # cat /etc/caddy/Caddyfile your.shitty.site:8123 {   proxy / localhost:9123 {     websocket     transparent   } } 이건 외부에서 8123으로 들어오는 걸 내부적으로 9123로 맞춰서 https 맞춰주는 reverse proxy. 만일 port를 생략하면 # cat /etc/caddy/Caddyfile your.shitty.site {   proxy / localhost:9123 {     websocket     transparent   } } 기본포트인 443으로 되어 https://your.shitty.site 로 접속이 된다. 요새 말썽이던 cloudflare 버리고 netlify domain이랑 caddy를 쓰니까 앓던 이가 빠진 느낌. 이 조합은 당분간 계속 써야겠다 싶다.

저지연 오디오 생성 LLVC 사용기

https://github.com/KoeAI/LLVC 써봄 CUDA 11.8 버전 쓰고 있어 pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 VC 없어서 설치 https://wikidocs.net/168848 참조 FAIRSEQ 관련 오류 https://github.com/facebookresearch/fairseq/issues/2908 pip install editdistance  설치 No CUDA runtime is found, using CUDA_HOME='C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8' 오류 멀쩡하게 있는데 안됨. pip install torch==2.1.0+cu118 torchvision==0.16.0+cu118 torchaudio==2.1.0+cu118 --extra-index-url https://download.pytorch.org/whl/cu118 다시 설치 set DISTUTILS_USE_SDK=1 이게 문젠가 설정하고 재도전 Using cached ruamel.yaml.clib-0.2.8-cp311-cp311-win_amd64.whl (118 kB) Building wheels for collected packages: fairseq   Building wheel for fairseq (pyproject.toml) ... error   error: subprocess-exited-with-error   × Building wheel for fairseq (pyproject.toml) did not run successfully.   │ exit code: 1   ╰─> [1829 lines of output]       No CUDA runtime is found, usi...