2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift Subscript関連でSegmentation faultによるビルドエラー

Posted at

Xcode 8.2, Swift 2.3, RealmSwift 1.1 のプロジェクトで、 xcodebuild Release build をすると以下の Segmentation fault が出ました。

1.	While type-checking 'a-method-name' at /path-to-workspace/a-swift-file.swift:48:5
2.	While type-checking expression at [/path-to-workspace/a-swift-file.swift:53:36 - line:53:74] RangeText="json["entry"] as? [[String: AnyObject]]"
3.	/path-to-workspace/build/RealmSwift.framework/Headers/RealmSwift-Swift.h:256:1: importing 'RealmSwiftObject::objectForKeyedSubscript:'

<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal (use -v to see invocation)
** BUILD FAILED **

Xcodeでのデバッグビルドでは問題なく、IDEでのwarning等もない、リリースビルド時もなぜか無事ビルドが通ることもある、という状況で困り果てていたのですが、以下のように修正することで解決しました。

修正前

let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
if let entry = json["entry"] as? [[String: AnyObject]] {

修正後

let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
if let json = json as? NSDictionary, entry = json["entry"] as? [[String: AnyObject]] {

修正の内容は、JSONObjectWithDataの戻り値を、 AnyObject から NSDictionary にキャストしているだけ。JSONObjectWithData() の戻り値は Objective-C の id 型 なので、あまりよく考えずに Objective-C 脳のまま Key-Value Coding していまっていました。

型推論まわりで(?) RealmSwiftとの組み合わせにより(?) Swift Compilerが死んでいるのかな?AnyObject って subscript は定義されていような気がするけど、なんでデバッグビルドでコンパイルエラーにならないんだろう。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?