notes

#swift

2020. 7. 6.
sourceViewController -> source

Implement Navigation을 진행하다가 아래 워닝을 만났다.

Screen Shot 2020-07-07 at 6 32 27

    //MARK: Actions
    @IBAction func unwindToMealList(sender: UIStoryboardSegue) {
-        if let sourceViewContoller = sender.sourceViewController as? MealViewController, let meal = sourceViewContoller.meal {
+        if let sourceViewContoller = sender.source as? MealViewController, let meal = sourceViewContoller.meal {
            // Add new meal.
            let newIndexPath = IndexPath(row: meals.count, section: 0)
            meals.append(meal)
            tableView.insertRows(at: [newIndexPath], with: .automatic)
        }
    }
    //MARK: Actions
    @IBAction func unwindToMealList(sender: UIStoryboardSegue) {
-        if let sourceViewContoller = sender.sourceViewController as? MealViewController, let meal = sourceViewContoller.meal {
+        if let sourceViewContoller = sender.source as? MealViewController, let meal = sourceViewContoller.meal {
            // Add new meal.
            let newIndexPath = IndexPath(row: meals.count, section: 0)
            meals.append(meal)
            tableView.insertRows(at: [newIndexPath], with: .automatic)
        }
    }

2020. 7. 5.
`Could not insert new outlet connection` error

문제

FoodTracker를 신나게 만드는 중이었는데, save 버튼을 MealViewController에 연결시키려던 순간!

위와 같은 메시지를 보여주면서 안되는 경우가 생겼다.

해결

  1. cmd+shift+K를 눌러 product를 clean 한다.
  2. cmd+R로 build + run을 한다.
  3. 한 번 해본다.
  4. 안되면 xcode를 재시작한다.

2020. 6. 29.
UIImagePickerControllerOriginalImage

FoodTracker 예제 중 Work with View Controllers 튜토리얼에 버그... 는 아니고 옛날 자료라서 지금 안돌아가는 게 있었다.

링크를 찾아 아래와 같이 해결

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        // The info dictionary may contain multiple representations of the image. You want to use the original.
-        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
+        guard let selectedImage = info[.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing as image, but was provided the following: \(info)")
        }

        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage

        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        // The info dictionary may contain multiple representations of the image. You want to use the original.
-        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
+        guard let selectedImage = info[.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing as image, but was provided the following: \(info)")
        }

        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage

        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
    }

2020. 6. 28.
vscode에서 swift language server 돌리기

xcode에선 vim key 바인딩, hover action 등등이 간단하게 안되는 것 같아 vscode에 개발 환경 꾸미는 게 가능할까 알아보았다.

  1. 원하는 위치에 sourcekit-lsp를 클론 받는다.
  2. $ cd sourcekit-lst/Editors/vscode
  3. $ npm run createDevPackage
  4. $ code --install-extension out/sourcekit-lsp-vscode-dev.vsix
  5. 공식엔 여기까지 하면 된다고 나와있긴 한데, vscode에서 Starting client failed Launching server using command sourcekit-lsp failed. 라며 에러가 발생했다.
  6. $ xcrun -f sourcekit-lspsourcekit-lsp가 설치된 경로를 알아내서 복사한다.
  7. vscode settings.json에 아래를 추가한다.
  8.   "sourcekit-lsp.serverPath": "<경로>"
      "sourcekit-lsp.serverPath": "<경로>"
  9. vscode를 reload 한다.
  10. Screen Shot 2020-06-28 at 14 34 30
  11. https://marketplace.visualstudio.com/items?itemName=vknabel.vscode-swiftlint 도 설치하면 좋다 카더라.