카테고리 없음

iOS프로그래밍실무(4/15)

59date 2025. 3. 26. 16:47
//
//  AppDelegate.swift
//  ddd
//
//  Created by LimJongHoon on 3/26/25.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

 

//
//  SceneDelegate.swift
//  ddd
//
//  Created by LimJongHoon on 3/26/25.
//

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }


}

 

//
//  ViewController.swift
//  ddd
//
//  Created by LimJongHoon on 3/26/25.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

 

함수를 정의할 때 사용하는 변수 이름이 parameter,
함수를 호출할 때 넘기는 실제 값이 argument.

 

func add(x: Int, y: Int) -> Int {
    return x + y
}
print(add(x:10, y:20))
var x = 10
print(type(of: x))
print(type(of: add))

함수의 자료형은 (매개변수, ) -> return 자료형

 

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

함수명: application(_:configurationForConnecting:options:)

 

 

numberOfRowsInSection이란?

- 테이블 뷰의 특정 섹션에 표시할 셀(행)의 개수를 반환하는 메서드.

 

 

class Man {
    var age : Int = 0 // swift에는 클래스 안에 있는 변수를 프로퍼티라고 하는데 무조건 값이 있어야 함
}

 

designated initializer란?

- Designated initializer(지정 생성자)는 클래스에서 모든 프로퍼티를 직접 초기화하고,

상위 클래스(superclass)의 초기화도 호출하는 기본 생성자

 

// Man 클래스 정의: 사람의 기본 정보를 저장하는 클래스
class Man {
    
    // 나이 저장하는 프로퍼티 (Int형)
    var age : Int
    
    // 몸무게 저장하는 프로퍼티 (Double형)
    var weight : Double

    // 나이와 몸무게를 출력하는 메서드
    func display(){
        print("나이=\(age), 몸무게=\(weight)")
    }

    // Man 클래스의 지정 생성자 (지정 초기화 메서드)
    init(age: Int, weight : Double){
        // 전달받은 age 값을 클래스의 age 프로퍼티에 저장
        self.age = age
        // 전달받은 weight 값을 클래스의 weight 프로퍼티에 저장
        self.weight = weight
    }
}

// Student 클래스 정의: Man 클래스를 상속받음
class Student : Man {
    
    // 이름 저장하는 프로퍼티 (String형)
    var name : String

    // 이름, 나이, 몸무게를 모두 출력하는 메서드
    func displayS() {
        print("이름=\(name), 나이=\(age), 몸무게=\(weight)")
    }

    // Student 클래스의 지정 생성자
    init(age: Int, weight : Double, name : String){
        // 자식 클래스의 프로퍼티(name)를 먼저 초기화해야 함
        self.name = name

        // 부모 클래스(Man)의 지정 생성자를 호출해서
        // 부모의 프로퍼티(age, weight)도 초기화함
        super.init(age:age, weight:weight)

        // ✅ 이 줄을 생략하면 에러 발생:
        // 'super.init' isn't called on all paths before returning from initializer
        // 자식 클래스에서 지정 생성자를 만들면,
        // 부모 클래스의 프로퍼티도 반드시 초기화해야 하므로
        // 부모의 생성자를 반드시 명시적으로 호출해야 함!
    }
}

// Student 클래스의 인스턴스를 생성하면서
// age: 20, weight: 65.2, name: "홍길동" 전달
var lee : Student = Student(age:20, weight:65.2, name:"홍길동")

// displayS() 메서드 호출 → 이름, 나이, 몸무게 출력
lee.displayS()

// 부모 클래스인 Man의 display() 메서드도 호출 가능 (상속받았기 때문)
lee.display()

프로토콜에 프로퍼티를 쓸 때는 { get } 또는 { get set }을 명시해야 함

 

swift에서 상속과 프로토콜을 동시에 사용하는 코드

// ✅ 프로토콜: 운전 가능한 사람은 반드시 drive() 메서드를 가져야 함
protocol Drivable {
    func drive()
}

// ✅ 부모 클래스: 모든 직원은 이름을 가지고 자기 소개를 할 수 있음
class Employee {
    var name: String

    init(name: String) {
        self.name = name
    }

    func introduce() {
        print("저는 \(name)입니다. 피자가게 직원이에요.")
    }
}

// ✅ 배달원 클래스: Employee를 상속 + Drivable 프로토콜 채택
class DeliveryMan: Employee, Drivable {
    func drive() {
        print("\(name): 오토바이 타고 배달 갑니다! 🛵")
    }
}

// ✅ 요리사 클래스: Employee만 상속 (운전은 안 함)
class Chef: Employee {
    func cookPizza() {
        print("\(name): 맛있는 피자를 만들고 있어요! 🍕")
    }
}