EnumTest

//: Playground – noun: a place where people can play

// Swift EnumType (enum)

// Kyoung Shin Park (2016 Fall)

import Cocoa

import Foundation

// enum

enum Geometry : Int, CustomStringConvertible {

case Sphere = 1

case Cone

case Cylinder

case RectangularPrism

case SquarePyramid

case IsoscelesTriangularPrism

 

init(_ name: String)

{

switch name {

case “Sphere”, “구”, “1”: self = .Sphere

case “Cone”, “원뿔”, “2”: self = .Cone

case “Cylinder”, “원기둥”, “3”: self = .Cylinder

case “RectangularPrism”, “사각기둥”, “4”: self = .RectangularPrism

case “SquarePyramid”, “정사각뿔”, “5”: self = .SquarePyramid

case “IsoscelesTriangularPrism”, “이등변삼각기둥”, “6”: self = .IsoscelesTriangularPrism

default: self = .Sphere

}

}

var description: String {

switch self {

case .Sphere: return “Sphere”

case .Cone: return “Cone”

case .Cylinder: return “Cylinder”

case .RectangularPrism: return “RectangularPrism”

case .SquarePyramid: return “SquarePyramid”

case .IsoscelesTriangularPrism: return “IsoscelesTriangularPrism”

}

}

static func getGeometryBy(index : Int) -> Geometry? {

switch index {

case 1: return .Sphere

case 2: return .Cone

case 3: return .Cylinder

case 4: return .RectangularPrism

case 5: return .SquarePyramid

case 6: return .IsoscelesTriangularPrism

default: return nil

}

}

// use anyGenerator to get a generator that can enumerate across your values

static func enumerate() -> AnyGenerator<Geometry> {

var nextIndex = Sphere.rawValue

return anyGenerator { Geometry(rawValue: nextIndex++) }

}

}

for geo in Geometry.enumerate() {

print(geo.description)

}

var geo = Geometry(“1”)

print(geo.description)

geo = Geometry(“원뿔”)

print(geo.description)

geo = Geometry(“Cylinder”)

print(geo.description)

geo = Geometry.RectangularPrism

print(geo.description)

geo = .SquarePyramid

print(geo.description)

if let g = Geometry.getGeometryBy(6) {

print(g.description)

}

 

ArrayDictionaryTest

// Array & Dictionary: Playground – noun: a place where people can play

// Kyoung Shin Park (2016 Fall)

import UIKit

var a = Array<String>()

var b = [String]()

var c = [Int](count: 3, repeatedValue: 1)

let animals = [“Giraffe”, “Cow”, “Dog”, “Cat”]

//animals.append(“Bird”) // due to let

var animal = animals[1]

//var animal2 = animals[5] // due to out of bounds

for animal in animals {

print(animal)

}

for (index, value) in animals.enumerate() {

print(“animals[\(index)]=\(value)”)

}

b.append(“Egg”)

b += [“Cocoa”, “Milk”, “Flour”]

b[1…3] = [“Apple”, “Butter”]

print(b)

c.insert(4, atIndex: 1)

c.insertContentsOf([5,6], at: 2)

c.removeAtIndex(1)

c.removeRange(0..<2)

c.replaceRange(0…1, with: [8,9,7])

let sorted = c.sort { $0 < $1 }

for value in sorted {

print(value)

}

// map, filter, reduce

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

var arr1 = arr.map { (x)->Int in return x+1 }

print(arr1)

var arr2 = arr.map { (x)->Int in return x*2 }

print(arr2)

var arr3 = arr.filter { (x)->Bool in return x%2 == 0 }

print(arr3)

var sum = arr.reduce(0, combine: { (result, x)->Int in return result+x } )

print(sum)

let sum2: Int = arr.reduce(0) { $0 + $1 }

print(sum2)

let stringified = arr.map { “\($0)” }

print(stringified)

// dictionary

var planets1 = Dictionary<String, Int>() //empty dictionary

var planets = [String: Int]() // empty dictionary

planets = [ “Mercury”:1, “Venus”:2

] // assign

planets[“Mars”] = 3 // append

for (key, value) in planets {

print(“\(key) = \(value)”)

}

planets[“Mars”] = 4 // replace

for (key, value) in planets {

print(“\(key) = \(value)”)

}

let earth = planets[“Earth”] // earth is an Int? (should be nil)

if earth == nil {

print(“Earth is not found”)

} else {

let earthID = earth! // use forced unwrapping !

print(“Earth = \(earthID)”)

}

planets[“Earth”] = 3 // append Earth

// optional binding

if let earthID = planets[“Earth”] {

print(“Earth = \(earthID)”)

}

ArrayTest

//: Playground – noun: a place where people can play

// Swift2 string, array

// Kyoung Shin Park (2016 Fall)

import UIKit

//string

let hello = “Hello”

let world = “World”

let smile = “?”

var h = hello + ” ” + world // “Hello World”

h += “, Swift” // “Hello World, Swift”

h.append(Character(“!”)) // “Hello World, Swift!”

var strH = h.stringByAppendingString(smile) // Hello World, Swift!

print(strH)

print(strH.uppercaseString)

var len = hello.characters.count // “Hello” 5

var len2 = hello.startIndex.distanceTo(hello.endIndex) // “Hello” 5

for s in hello.characters { // using foreach

print(“\(s)”)

}

for i in 0..<hello.characters.count { // using String.Index & range

print(“\(i): \(hello[hello.startIndex.advancedBy(i)])”)

}

for (index, value) in hello.characters.enumerate() { // using array & tuple

print(“\(index): \(value)”)

}

// array

func fillArray(inout arr: [Int]) {

    arr.appendContentsOf([1, 2, 3])

}

var myArray : [Int] = []

fillArray(&myArray)

print(myArray)

 

var a = Array<String>()

var b = [String]()

var c = [Int](count: 3, repeatedValue: 1)

var animals = [“Giraffe”, “Cow”, “Dog”, “Cat”]

animals.append(“Bird”)

var animal = animals[1]

for animal in animals { // using foreach

print(animal)

}

for i in 0..<animals.count { // using index & range

print(“animals[\(i)]=\(animals[i])”)

}

for (index, value) in animals.enumerate() { // using tuple & enumerate

print(“animals[\(index)]=\(value)”)

}

b.append(“Egg”)

print(b)

b += [“Cocoa”, “Milk”, “Flour”]

print(b)

b[1…3] = [“Apple”, “Butter”]

print(b)

c.insert(4, atIndex: 1)

print(c)

c.insertContentsOf([5,6], at: 2)

print(c)

c.removeAtIndex(1)

print(c)

c.removeRange(0..<2)

print(c)

c.replaceRange(0…1, with: [8,9,7])

print(c)

let sorted = c.sort { $0 < $1 }

for value in sorted {

print(value)

}

// http://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/

// collections – map, filter, reduce

let odd: [Int] = sorted.filter { $0 % 2 != 0}

print(odd)

let stringified: [String] = sorted.map { “\($0)” }

print(stringified)

let squares: [Int] = sorted.map { $0 * $0 }

print(squares)

let sum: Int = [1, 2, 3].reduce(0) { $0 + $1 } // adds up the numbers in the Array

print(sum)

StringTest

//: Playground – noun: a place where people can play

// Swift2 string blog

// https://developer.apple.com/swift/blog/?id=30

// Kyoung Shin Park (2016 Fall)

import Cocoa

var letters: [Character] = [“c”, “a”, “f”, “e”]

var cafe = String(letters)

print(letters.count)

print(cafe)

print(cafe.characters.count)

// print char of string “cafe”

for c in cafe.characters {

print(c)

}

let acuteAccent : Character = “\u{0301}”

cafe.append(acuteAccent)

print(cafe)

print(cafe.characters.count)

print(cafe.characters.last!)

cafe.characters.contains(“e”)

cafe.characters.contains(“´”)

cafe.characters.contains(“é”)

// In Swift, strings are considered equal regardless of whether they are constructed from decomposed or precomposed character sequences:

let decomposed = “\u{1100}\u{1161}” // ᄀ + ᅡ

let precomposed = “\u{AC00}” // 가

decomposed == precomposed // true

let string1 = “”

var string2: NSString = “Hello”

switch (string2) {

case let string2 as NSString:

print(“string2 is NSString”)

case let string2 as String:

print(“string2 is String”)

default:

print(“None of those”)

}

var len = string2.length

var string3 = String(“Hello”)

string3 += ” World”

string3.append(Character(“!”))

var string4 = String(count: 7, repeatedValue: Character(“A”))

var string5 = NSString()

var string9 = NSString(string: “Hello”)

var string6 = NSMutableString(capacity: 10)

var total = 100

var string7 = “\(string3)! total=\(total)”

var string8: NSString = string7

//var string10: String = string3.stringByAppendingFormat(” append %@”, total)

//print(string10)

string5 = string2.stringByAppendingFormat(” append %@”, string8)

print(string8)

print(string8.description)

var str = “Hello”

var world = ” World”

var smile = “?”

var mark = “!~”

var combined = str + mark

var combined2 = (str + world).stringByAppendingString(smile)

var len2 = combined2.characters.count

// length

var length = combined.characters.count

var len3 = combined.startIndex.distanceTo(combined.endIndex)

var len4 = (combined as NSString).length

// print char of string

for char in combined.characters {

print(char)

}

print(combined2.lowercaseString)

print(combined2.uppercaseString)

print(combined2.capitalizedString)

if let range = combined.rangeOfString(“ello”) { // contains

print(“ello”)

}

else {

print(“none”)

}

var trail = combined.substringWithRange(Range<String.Index>(start: str.endIndex, end: combined.endIndex))

print(“combined = \(combined)”)

let startIndex = combined.startIndex.advancedBy(2)

let endIndex = combined.startIndex.advancedBy(5)

//combined.insert(“^”, atIndex: combined.endIndex)

//let question : Character = “?”

//combined.append(question)

//print(combined)

combined.insert(“a”, atIndex: startIndex)

combined.insertContentsOf(“bc”.characters, at: startIndex.successor())

combined.removeAtIndex(combined.endIndex.predecessor())

print(combined)

let substring = combined[startIndex..<endIndex]

print(“substring = \(substring)”)

var sub2 = combined.substringWithRange(Range<String.Index>(start: startIndex, end: endIndex))

print(sub2)

var myString = “Seoul, New York, Tokyo LA”

var myArray = myString.componentsSeparatedByString(“,”)

for s in myArray {

print(s)

}

var myArray2 = myString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: “, “))

for s in myArray2 {

print(s)

}

var num = “56.25”

if let range = num.rangeOfString(“.”) {

let wholeNum = num[num.startIndex..<range.startIndex]

print(“wholeNum=\(wholeNum)”)

num.replaceRange(num.startIndex..<range.startIndex, with: “32”)

print(“num=\(num)”)

num.removeRange(num.startIndex..<range.startIndex)

print(“num2=\(num)”)

}

let animals = [“Giraffe”, “Cow”, “Dog”, “Cat”]

//animals.append(“Bird”)

var animal = animals[2]

for (index, value) in animals.enumerate() {

print(“animals[\(index)]=\(value)”)

}

DataType

//: Playground – noun: a place where people can play

// Swift2 DataType (constant, variable)

// Swift2 DataType (int, float, double, char, string, tuple, type inference, type casting)

// Kyoung Shin Park (2016 Fall)

import Cocoa

// constant

let x = 16 // Int (type inference)

let y : Double = -2.3 // Double

let z = -1.2 // Double (type inference)

let w : Float = -1.7 // Float

let str = “Hello, playground” // String (type inference)

let c1 = “A” // String (type inference)

let c2 : Character = “A” // Character

print(“x=\(x) y=\(y) z=\(z) w=\(w) y+z=\(y+z)”)

print(“x+y+z+w=\(Double(x)+y+z+Double(w))”) // type casting

print(“str=\(str) c1=\(c1) c2=\(c2)”)

// tuple

let (s, t, r, u, v) = (“ABC”, “D”, 1, 3.5, true) // tuple

print(“s=\(s) t=\(t) r=\(r) u=\(u) v=\(v)”)

let tupleValues = (“ABC”, “D”, 1, 3.5, true) // tuple

print(“\(tupleValues.0) \(tupleValues.1) \(tupleValues.2) \(tupleValues.3) \(tupleValues.4)”)

// variable

var isUsed = true // Bool (type inference)

if isUsed {

print(“isUsed=\(isUsed)”)

}

var a = 10 // Int (type inference)

print(“a=\(a)”)

a = 20

print(“a=\(a)”)

var b : UInt // UInt declaration

b = 30 // initialization

print(“b=\(b)”)

print(“a+b=\(a+Int(b))”)

for i in a…Int(b) {

print(i)

}

for j in 0..<5 {

print(j)

}

var str1 = “Hello” // String (type inference)

print(“str1=\(str1)”)

str1 += ” World” // String += operator

print(“str1=\(str1)”)

for s in str1.characters { // using foreach

print(“\(s)”)

}

for i in 0..<str1.characters.count { // using String.Index & range

print(“str1[\(i)]=\(str1[str1.startIndex.advancedBy(i)])”)

}

var charView = str1.characters

for (index, value) in charView.enumerate() { // using array & tuple

print(“str1[\(index)]=\(value)”)

}

How to download Xcode dmg

http://stackoverflow.com/questions/10335747/how-to-download-xcode-4-5-6-7-8-and-get-the-dmg-file

You can find the DMGs for Xcode and other development tools on https://developer.apple.com/download/more/ (requires Apple ID to login).

You must login to have a valid session before downloading anything below.

*(Newest on top. For each minor version (6.3, 5.1, etc.) only the latest revision is kept in the list.)