9. let
json
=
try
NSJSONSerialization.JSONObjectWithData(data,
options:
NSJSONReadingOptions.MutableContainers)
if
let
query
=
json["query"]
as?
NSDictionary
{
if
let
normalized
=
query["normalized"]
as?
NSArray
{
if
let
firstNormarized
=
normalized[0]
as?
NSDictionary
{
if
let
to
=
firstNormarized["to"]
as?
String
{
//
取り出せた!
}
}
}
}
{"batchcomplete":"","query":{"normalized":[{"from":"swift","to":"Swift"}],"pages":
{"927971":{"pageid":927971,"ns":0,"title":"Swift"}}}}
AnyObject
型
10. import
SwiftyJSON
let
json
=
JSON(data:data)
if
let
normarizedKeyword
=
json[“query"]["normalized"][0]["to"]
.string
{
...
}
{"batchcomplete":"","query":{"normalized":[{"from":"swift","to":"Swift"}],"pages":
{"927971":{"pageid":927971,"ns":0,"title":"Swift"}}}}
JSON
型
String
型 JSON
型
12. json["query",
"normalized",
0,
“to"]
let
json
=
JSON(["sample":
[1,
"a",
true],
"str":
"abc"])
json["sample"]
json.map({
$0
})
json.first
for
element
in
json
{
print(element)
}
let
jsonStr:JSON
=
"aaa"
let
jsonBool:JSON
=
true
for
element
in
jsonBool
{
print(element)
}
//
エラーでない
if
(json)
{}
JSON
型
JSON
型
13. struct
SampleStruct
{}
let
sampleStruct
=
SampleStruct()
sampleStruct["sample"]
//
エラー
sampleStruct[“sample"]["sample"]
//
エラー
sampleStruct.map({
$0
})
//
エラー
sampleStruct.first
//
エラー
for
_
in
sampleStruct.startIndex
..<
sampleStruct.endIndex
{}
//
エラー
for
element
in
sampleStruct
{
print(element)
}
//
エラー
if
(sampleStruct)
{}
//
エラー
22. 内部でキャスト
switch
newValue
{
case
let
number
as
NSNumber:
if
number.isBool
{
_type
=
.Bool
}
else
{
_type
=
.Number
}
self.rawNumber
=
number
case
let
string
as
String:
_type
=
.String
self.rawString
=
string
case
_
as
NSNull:
_type
=
.Null
case
let
array
as
[AnyObject]:
_type
=
.Array
self.rawArray
=
array
case
let
dictionary
as
[String
:
AnyObject]:
_type
=
.Dictionary
self.rawDictionary
=
dictionary
24. json["query",
"normalized",
0,
“to"]
let
json
=
JSON(["sample":
[1,
"a",
true],
"str":
"abc"])
json["sample"]
json.map({
$0
})
json.first
for
element
in
json
{
print(element)
}
if
(json)
{}
25. struct
SampleStruct
{
var
object:AnyObject?
}
//
ここに実装を追加していきます
let
sampleStruct
=
SampleStruct()
//
以下は今の状態では全てエラーです
sampleStruct["sample"]
sampleStruct["sample"]["sample"]
sampleStruct.map({
$0
})
for
element
in
sampleStruct
{
print(element)
}
sampleStruct.first
for
_
in
sampleStruct.startIndex
..<
sampleStruct.endIndex
{}
if
(sampleStruct)
{}
while
(!sampleStruct)
{}
26. subscript
json[“sample”], json[0]
private
subscript(key
key:
String)
-‐>
JSON
{
get
{
var
r
=
JSON.null
if
self.type
==
.Dictionary
{
if
let
o
=
self.rawDictionary[key]
{
r
=
JSON(o)
}
else
{
r._error
=
NSError(domain:
ErrorDomain,
code:
ErrorNotExist,
userInfo:
[NSLocalizedDescriptionKey:
"Dictionary["(key)"]
does
not
exist"])
}
//
以下略
27. SequenceType
json.map({ $0 }), for var element in json
protocol
SequenceType
{
typealias
Generator
:
GeneratorType
public
func
generate()
-‐>
Self.Generator
}
struct
SampleGenerator
:
GeneratorType
{
typealias
Element
mutating
func
next()
-‐>
Self.Element?
}
28. SequenceType
json.map({ $0 }), for var element in json
• プロトコル
• 機能: 次の要素へのアクセスと遷移ができる
let
array
=
[1,
2,
3]
array.map({
$0
})
for
var
element
in
array
{}
29. SequenceType
json.map({ $0 }), for var element in json
• プロトコル
• 機能: 次の要素へのアクセスと遷移ができる
1 を
返す状態
2 を
返す状態
3 を
返す状態
nil
next next next
30. SequenceType
json.map({ $0 }), for var element in json
protocol
SequenceType
{
typealias
Generator
:
GeneratorType
public
func
generate()
-‐>
Self.Generator
}
//
イテレータのようなもの
struct
SampleGenerator
:
GeneratorType
{
typealias
Element
mutating
func
next()
-‐>
Self.Element?
}
31. CollectionType
json.first,
for _ in sampleStruct.startIndex ..<
sampleStruct.endIndex {}
protocol
CollectionType
{
typealias
Index
var
startIndex:
Self.Index
var
endIndex:
Self.Index
subscript(i:
Self.Index)
-‐>
Self.Generator.Element
}
1 を
返す状態
2 を
返す状態
3 を
返す状態
nil
next next next
SequenceType
を継承したプロトコル
startIndex endIndex[1]