LoginSignup
15
19

More than 5 years have passed since last update.

cocoapodsセットアップからSwiftyJSON動作確認まで

Posted at

cocoapods インストール

sudo gem update --system
sudo gem install cocoapods

一度ターミナルを開き直す

pod --version
pod setup

参考
http://www.slideshare.net/taichikanako/swift-47934385

プロジェクトで初期設定

cd プロジェクトのディレクトリ
pod init

Podfileが作成されている

SwiftyJSONを使うようにPodfileを修正

https://github.com/SwiftyJSON/SwiftyJSON で書き方を見ながら

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'

use_frameworks!
pod "SwiftyJSON", ">= 2.2"

target 'MyHttp' do
end

target 'MyHttpTests' do
end

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

pod install

Xcodeで
Product > Buildで一度ビルドする.
やらないと、この後のimport SwiftyJSONでエラーになる。

実装

iPhoneでボタンをタッチするとJSONを読み込むというサンプル

ViewController.swift
import UIKit
import SwiftyJSON

class ViewController: UIViewController {

    @IBAction func StartButton(sender: AnyObject) {
        var toJSON: JSON =  ["name": "Jack", "age": 25]
        println(toJSON)

        var jsonString = "{\"あああ\": 25}"
        var dataFromString = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
        var json = JSON(data: dataFromString!)
        println(json["あああ"])
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

おしまい

15
19
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
15
19