コンピュータ

...now browsing by category

 

XcodeでSwiftのリファクタリングができないから、renameの代わりにターミナルのコマンドで一括置換してみた

木曜日, 9月 17th, 2015

Xcode7の正式版が出たというのに、未だにSwiftのリファクタリング機能が実装されていません。特にrenameはよく使うので困りものです。さすがにちまちま置換していくのはとても面倒なので、ターミナルのコマンドで一括置換しました。

find -E . -regex ".*\.(swift|storyboard)" | xargs perl -i -pe 's/\bOldName\b/NewName/g'

これでカレントディレクトリ配下の *.swift ファイルおよび *.storyboard ファイル内の、「OldName」を「NewName」に置換できます。

「.*\.(swift|storyboard)」の部分で対象とするファイルを指定しています。「-E」「-regex」 は拡張正規表現を使うためのオプションで、両方必要なようです。

「OldName」の前後の「\b」は単語の区切りを示します。これがあると「HogeOldName」や「OldNameMoge」などは対象外になります。

最初、perlの代わりにsedを使おうと思っていたのですが、正規表現で「\b」が使えなかったり、バックアップファイルが残ってしまったりするので、perlに変えました。

Frameworkのテストコードで”Library not loaded: @rpath/libswiftCoreAudio.dylib”

木曜日, 8月 27th, 2015

iOSでFrameworkのテストコードを書いたのですが、テストを実行しようとすると、ビルドは成功するのですが、テスト実行時に以下のエラーが出てテストが行われませんでした。

2015-08-27 08:35:04.489 xctest[1176:62057] The test bundle at /Users/who/Library/Developer/Xcode/DerivedData/XxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest could not be loaded because an unanticipated error occurred.
2015-08-27 08:35:04.490 xctest[1176:62057] Detailed error information: Error Domain=NSCocoaErrorDomain Code=3587 “The bundle “xxxTests” couldn’t be loaded because it is damaged or missing necessary resources.” (dlopen_preflight(/Users/who/Library/Developer/Xcode/DerivedData/XxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest/xxxTests): Library not loaded: @rpath/libswiftCoreAudio.dylib
Referenced from: /Users/who/Library/Developer/Xcode/DerivedData/XxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxx.framework/xxx
Reason: image not found) UserInfo=0x7f9a39422c50 {NSLocalizedFailureReason=The bundle is damaged or missing necessary resources., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest/xxxTests, NSDebugDescription=dlopen_preflight(/Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest/xxxTests): Library not loaded: @rpath/libswiftCoreAudio.dylib
Referenced from: /Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxx.framework/xxx
Reason: image not found, NSBundlePath=/Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest, NSLocalizedDescription=The bundle “xxxTests” couldn’t be loaded because it is damaged or missing necessary resources.}

*** Test session exited(1) without checking in. If you believe this error represents a bug, please attach the log file at /var/folders/nt/x60j01ps3r3dds9ldvppd5xm0000gn/T/com.apple.dt.XCTest-status/Session-2015-08-27_08:35:01-Z4Gt8p.log

  • framework内でAVFoundationにリンクしています。
  • CoreLocationを使用しています。
  • サンプルアプリに組み込んだframeworkは正常に動作していました。

cleanしたり、再起動したりしたのですが、改善しませんでした。色々試した結果、以下のBuild Settingsを変更したところ、正常に動くようになりました。

Embedded Content Contains Swift Code = NO -> YES

ですが、これで動くようになった理由がわかりません。
新しくframeworkを作るとデフォルトでNOになっているのは確認しました。

NSRange を Range<String.Index> に変換

火曜日, 4月 7th, 2015

Swift でちょっと苦労したのでメモ。

UITextFieldDelegate で返される NSRange を String の操作に使おうとしたら、そのままでは使えませんでした。調べてみると String の操作メソッドでは Range<String.Index> が使わていました。文字の位置を表す型として int や NSInteger ではなく、String.Index という専用の型が設けられたようです。

int から String.Index には直接変換できないので、advance関数を利用します。苦労の末に以下の方法で NSRange から Range<String.Index> への変換が出来ました。

    func rangeWith(range: NSRange) -> Range<String.Index> {
        var string = "dummy"
        var startIndex = advance(string.startIndex, range.location)
        var endIndex = advance(startIndex, range.length)
        return Range(start: startIndex, end: endIndex)
    }

ダミーのString使ったりするのはイマイチなので、なんか良い方法はないかなー

中古の MacBook Air がやってきた

木曜日, 3月 5th, 2015

IMG_2485

子ども2号が Mac Book Air を買いました。
中古の11インチ、2010年モデルです。

秋葉原とか行って色々と見て回ったのですが、どこも値段は5万円くらいが下限でした。

地元の PC DEPO にも下限の値段で売っている個体があったので、そちらで買う事にしました。
展示されていて、動作していたので、動きやディスプレイのドット欠けを確認して買えたのが良かったです。
保証期間内に何かあったときに返品しに行くのも楽だしね。

これでうちにある MacBook は3台になりました。
それと Linux PC が1台。
ちなみに Windows PC はありません。:-P

Xcodeプロジェクトの.gitignore

土曜日, 1月 10th, 2015

色々な案があって迷ったけど、一番自分に合っているのをまとめておく。


# Mac
.DS_Store
*.swp
!.gitkeep

# Xcode
build/*
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
*.xcworkspace
!default.xcworkspace
xcuserdata
profile
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# VCSのメタデータが含まれ、コミットしてしまうとmergeが困難になるなどの問題が発生する
# http://stackoverflow.com/questions/18340453/should-xccheckout-files-in-xcode5-be-ignored-under-vcs
*.xccheckout

#CocoaPod
Pods/*

ベースはXcodeでiOSアプリ開発をする時の.gitignoreを使わせてもらった。

Podfile.lockは迷ったけど、Xcode&gitで開発する時の.gitignoreのコメント欄にikesyoさんが書いた以下の文を参考にして、gitで管理する事にした。

lockファイルが存在する場合、そこに書かれているバージョンを参照してダウンロード・インストールするので、複数人の間でバージョンを固定できます。

ライブラリのバージョンアップはpod update(新しいバージョンが利用できる場合、lockファイルも更新されます)、新しいバージョンが出ていないか確認したい場合には pod outdatedを使えばいいですね。

【2015/09/17】
*.xccheckoutを追加