//code1
public var number : Int = 0
class MyMarvel {
var heroName : String
init(_ heroName : String) {
self.heroName = heroName
number = number+1
show()
}
func ordinalCheck() -> String {
if number == 1 {
return "st"
}
else if number == 2 {
return "nd"
}
else if number == 3 {
return "rd"
}
else {
return "th"
}
}
func show() {
if number % 2 == 1 {
print("Hello \(heroName), you are \(number)\(ordinalCheck()).")
}else{
print("Hi \(heroName), you are \(number)\(ordinalCheck()).")
}
}
}
var hero1 : MyMarvel = MyMarvel("아이언맨")
var hero2 : MyMarvel = MyMarvel("토르")
var hero3 : MyMarvel = MyMarvel("헐크")
var hero4 : MyMarvel = MyMarvel("캡틴")
var hero5 : MyMarvel = MyMarvel("블랙위도우")
var hero6 : MyMarvel = MyMarvel("호크아이")
var hero7 : MyMarvel = MyMarvel("타노스")
var hero8 : MyMarvel = MyMarvel("앤트맨")
var hero9 : MyMarvel = MyMarvel("닥터스트레인지")
var hero10 : MyMarvel = MyMarvel("비전")
//code2
import Foundation
class Player{
var Playername : String
init (name : String){
Playername = name
}
var name : String {
get {
return Playername
}
set {
Playername = newValue
}
}
func displayname() -> String {
return "\(Playername) 선수"
}
}
class Friend : NSCopying{
var Friendname : String
init (name : String){
Friendname = name
}
func copy(with zone : NSZone? = nil) -> Any {
return Friend(name: Friendname)
}
var name : String {
get {
return Friendname
}
set{
Friendname = newValue
}
}
func displayname() -> String {
return "\(Friendname) 선수"
}
}
let playerOne : Player = Player(name : "호날두")
let playerTwo : Player = playerOne
playerOne.name = "메시"
print("P1 : \(playerOne.displayname())") //P1 : 메시 선수
print("P2 : \(playerTwo.displayname())") // P2 : 메시 선수
var friendOne : Friend = Friend(name : "둘리")
var friendTwo : Friend = friendOne.copy() as! Friend
friendOne.name = "도우너"
print("F1 : \(friendOne.displayname())") //F1: 도우너선수
print("F2 : \(friendTwo.displayname())") //F2 : 둘리선수
NSCopying 프로토콜을 사용하면 깊은복사 사용할 수 있음
'PROGRAMMING CODE > SWIFT' 카테고리의 다른 글
[UIkit] 음성출력예제 (0) | 2023.06.12 |
---|---|
[Swift] 기초 예제 5 (딕셔너리) (0) | 2023.06.08 |
[Swift] 기초 예제 2 (함수연습) (0) | 2023.05.31 |
[Swift] 기초 예제 1 (0) | 2023.05.31 |
[Swift] 키체인 오류 (0) | 2022.07.18 |