LoginSignup
9
9

More than 5 years have passed since last update.

簡単にJSONのパースができるSwiftyJSONのインストール方法

Last updated at Posted at 2017-05-14

JSONのパースは普通にswiftでやるとかなり煩雑なコードになるのでSwiftyJSONを用いて行う.
詳細は次を参照: https://github.com/SwiftyJSON/SwiftyJSON

まずはSwiftyJSON入れるのに必要なライブラリ等入れる

以下参照:

http://qiita.com/daikuro/items/1e3758f58783e12e8a52

https://cocoapods.org/

1. cocoapodsのインストール (ruby必要, 2017/5/10 v2.4.1p11)

$ gem install cocoapods

2.Teminal再起動

3. cocoapodsがインストールされているか確認&setup(setupは10分弱かかる)

$ pod --version -> 1.2.1

4. 初期設定

プロジェクトのあるディレクトリ (.xcodeproj)で
$ pod initを実行するとPodfilefが作成される.

SwiftyJSONを実装する

以下参照

http://qiita.com/daikuro/items/1e3758f58783e12e8a52

https://github.com/SwiftyJSON/SwiftyJSON#integration

1. PodfileをSwiftJSON用に修正

(Before)

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'MySampleApp' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for MySampleApp

end

(After)

platform :ios, '10.3'
use_frameworks!

target 'MySampleApp' do
  pod 'SwiftyJSON'
end

2. ライブラリのインストール

$ pod install
Analyzing dependencies
Downloading dependencies
Installing SwiftyJSON (3.1.4)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `MySampleApp.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

3.この後xcodeでbuildする

ちなみに.xcodeprojからビルドするとfailedする..xcworkspaceのファイルができているのでそこからbuildする.

参照: http://qiita.com/sutchan/items/0a78042261ca3f0a7d91

4.あとはViewController.swift等にimport SwiftyJSONを追加すれば使えるようになる

[追記(2017/6/13)] SwiftPM経由でSwiftyJSON導入

1. 該当ディレクトリに移動

$ cd MySampleApp

2. Setup

$ swift package init --type=executable
Creating executable package: BudgetBrain
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/

3. Package.swiftにパッケージ指定

Package.swift
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", versions: Version(1, 0, 0)..<Version(3, .max, .max)),
    ]
)

4. ビルドする

$ swift build
Fetching https://github.com/SwiftyJSON/SwiftyJSON.git
Cloning https://github.com/SwiftyJSON/SwiftyJSON.git
Resolving https://github.com/SwiftyJSON/SwiftyJSON.git at 3.1.4
Compile Swift Module 'SwiftyJSON' (1 sources)
Compile Swift Module 'YOUR_PROJECT_NAME' (1 sources)
Linking ./.build/debug/YOUR_PROJECT_NAME
9
9
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
9
9