2014年8月17日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成: 1つのことだけどうまくやる)、ポケットコード(p.105)をSwiftで考えてみる。

ポケットコード(p.105)

コード(Xcode)

string.swift

//
//  string.swift
//  string
//
//  Created by kamimura on 8/10/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

extension String {
    var swapcaseString:String {
    var result: String = ""
        for ch in self {
            let s = String(ch)
            result += s.uppercaseString == s ? s.lowercaseString : s.uppercaseString
        }
        return result
    }
    var isUpper:Bool { return self.uppercaseString == self }
    var isLower:Bool { return self.lowercaseString == self }
    var length:Int { return (self as NSString).length }
    var strip:String { return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())}
    func count(sub:String) -> Int {
        var result:Int = 0
        var s = self
        let index:String.Index = "a".endIndex
        while s != "" {
            if s.hasPrefix(sub) {
                result += 1
                s = s.substringFromIndex(sub.endIndex)
            } else {
                s = s.substringFromIndex(index)
            }
            
        }
        return result
    }
    func find(sub:String, start:Int = 0, end:Int? = nil) -> Int {
        var s = self as NSString
        var s_temp = s.substringFromIndex(start)
        let end_temp:Int = end ? end! : s.length
        for i in start..<end_temp {
            if (s_temp.hasPrefix(sub)) {
                return i
            }
            s_temp = s.substringFromIndex(i + 1) as NSString
        }
        return -1
    }
    func replace(old_str:String, new_str:String) -> String {
        return self.stringByReplacingOccurrencesOfString(old_str, withString: new_str)
    }
    func slice(start:Int=0, end:Int?=nil) -> String {
        let s = self as NSString
        var new_end:Int
        if (end) {
            new_end = end!
        } else {
            new_end = s.length
        }
        return  s.substringWithRange(NSRange(location: start, length:new_end - start))
    }
    func split(sep:String = "") -> [String] {
        var result:[String] = []
        if (sep == "") {
            for ch in self {
                result.append(String(ch))
            }
            return result
        }
        var s:String = self
        var len = sep.length
        var temp:String = ""
        while s != "" {
            if s.hasPrefix(sep) {
                if temp != "" {
                    result.append(temp)
                    temp = ""
                }
                s = s.slice(start: len)
            } else {
                temp += s.slice(start: 0, end: 1)
                s = s.slice(start: 1)
            }
        }
        if temp != "" {
            result.append(temp)
        }
        return result
    }
    var reversed:String { return "".join(self.split().reverse()) }
}

main.swift

//
//  main.swift
//  sample105
//
//  Created by kamimura on 8/17/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

// 標準入力の代わり
let csv:String = "42.363400,-710.098465,Speed = 21\n42.363327,-71.097588,Speed = 23\n30.0,-70.0,Speed = 20\n42.363255,-71.096710,Speed = 17\n"
var started:Bool = false

println("data=[")

for line:String in csv.split(sep: "\n") {
    let values = line.split(sep: ",")
    if started {
        println(",")
    } else {
        started = true
    }
    print("{latitude: \(values[0]), longitude: \(values[1]), info: '\(values[2])'}")
}

println("\n]")

入出力結果(Console Output)

data=[
{latitude: 42.363400, longitude: -710.098465, info: 'Speed = 21'},
{latitude: 42.363327, longitude: -71.097588, info: 'Speed = 23'},
{latitude: 30.0, longitude: -70.0, info: 'Speed = 20'},
{latitude: 42.363255, longitude: -71.096710, info: 'Speed = 17'}
]
Program ended with exit code: 0

0 コメント:

コメントを投稿