5. • Array에 데이터 넣기
• Array<Int>(repeating:0, count:0) 로 선언한 경우
• 예 : 배열 데이터 수정과 내용 출력
• Range를 이용한 갱신
arr[2] = 1;
arr[2…3] = [5,5];
Array
Swift2 : (count:5, repeatedValue:0)
6. • Array에 데이터 넣기
• 다음과 같이 빈 Array를 생성한 경우
• 데이터 추가
• empty array 할당
var arrInt : Array<Int> ;
arrInt = Array();
var arrInt : [Int];
arrInt = [Int]();
arrInt.append(3);
arrInt = [];
arrInt += [3] ;
shoppingList.append(“Flour”);
shoppingList += [“Flour”];
Array
7. • Array에 데이터 삽입과 삭제
• 데이터 삽입하기
• 데이터 삭제하기
arrInt.insert( 5, at : 0 );
arrInt.remove( at : 4 );
arrInt.removeLast( );
Array
Swift2
8. • isEmpty
• Array 의 empty 상태 파악하기
• enumerated
• ( index, value) tuple 형태로 enumeration
if arrInt.isEmpty {
print( “empty”);
}
for (index, value) in shoppingList.enumerated()
{
print("Item (index + 1): (value)")
}
Array
10. Set
• Set 변수와 초기화
• Set 초기화
// letters is now an empty set, but is still of type Set<Character>
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
// favoriteGenres has been initialized with three initial items
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
11. Set
• Set 개수 세기
• isEmpty
print("I have (favoriteGenres.count) favorite music genres.")
// Prints "I have 3 favorite music genres.
if favoriteGenres.isEmpty {
print("As far as music goes, I'm not picky.")
} else {
print("I have particular music preferences.")
}
// Prints "I have particular music preferences."
12. • Set 삽입과 삭제
• contains
favoriteGenres.insert("Jazz")
// favoriteGenres now contains 4 items
if let removedGenre =
favoriteGenres.remove("Rock") {
print("(removedGenre)? I'm over it.")
} else {
print("I never much cared for that.")
}
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
} else {
print("It's too funky in here.")
}
// Prints "It's too funky in here."
Set
17. Dictionary
• Dictionary 생성
• Dictionary에 값 넣고 출력하기
var namesOfIntegers = [Int: String]()
// namesOfIntegers is an empty [Int: String] dictionary
nameOfIntegers[3] = "three";
for i in nameOfIntegers
{
print( "(i.0) (i.1) " );
}
18. Dictionary
• Dictionary 생성과 초기화
• type 없는 생성
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
19. Dictionary
• 데이터 갱신
• 데이터 삭제
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was (oldValue).")
}
// Prints "The old value for DUB was Dublin."
if let removedValue = airports.removeValue(forKey: "DUB") {
print("The removed airport's name is (removedValue).")
} else {
print("The airports dictionary does not contain a value for DUB.")
}
// Prints "The removed airport's name is Dublin Airport."
20. Dictionary
• Iterating (key, value ) tuple
• key의 enumeration
for (airportCode, airportName) in airports {
print("(airportCode): (airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow
for airportCode in airports.keys {
print("Airport code: (airportCode)")
}
// Airport code: YYZ
// Airport code: LHR
for airportName in airports.values {
print("Airport name: (airportName)")
}
// Airport name: Toronto Pearson
// Airport name: London Heathrow
21. Dictionary
• key 또는 value의 array 만들기
let airportCodes = [String](airports.keys)
// airportCodes is ["YYZ", "LHR"]
let airportNames = [String](airports.values)
// airportNames is ["Toronto Pearson", "London Heathrow"]
24. 실습
• 컴파일된 Mac OS X 용 실행 파일 만들기
• 실행
• 실행 파일의 위치
• /Users/사용자이름/Library/Developer/
Xcode/DerivedData/프로젝트이름
/Build/Products/Debug
print( “name“);
var name = readLine();
print( “Hello (name!)~~~“);