Swift 계산기 만들기 (UIKit-Stroyboard)

2024. 6. 28. 16:36·Swift Study/Storyboard

 

 

 

 

 

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var display: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        display.text = "0"
    }
    
    // 숫자 버튼 액션 메서드
    @IBAction func numberTapped(_ sender: UIButton) {
        if let str = display.text, let text = // let text가 operatorSymbol이 되야함
            sender.titleLabel?.text {
            if let num = Int(str), num == 0 {
                display.text = text
            } else {
                display.text = str + text
            }
        }
    }
 
    // 연산자 버튼 액션 메서드
    
    @IBAction func operatorTapped(_ sender: UIButton) {
        // 연산자 버튼을 눌렀을 때 실행되는 액션 메서드
        
        if let operatorSymbol = sender.titleLabel?.text, let displayText = display.text { // 옵셔널을 해제해주기 위해 let으로 변수 선언
            display.text = displayText + operatorSymbol
            
        }
        
    }
    // 등호 버튼 액션 메서드
    
    @IBAction func equalTapped(_ sender: UIButton) {
        
        // 등호(=) 버튼을 눌렀을 때 실행되는 액션 메서드
        if let displayText = display.text {
            
            let displayText2 = displayText.replacingOccurrences(of: "×", with: "*").replacingOccurrences(of: "÷", with: "/") // 곱하기 나누기를 with로 나온거로 바꿔줌
            
            let expression = NSExpression(format: displayText2)
            
            if let result = expression.expressionValue(with: nil, context: nil) as? Int {
                display.text = String(result)
            } else {
                display.text = "Error"
            }
        }
        
    }
    
    @IBAction func deleteTapped(_ sender: UIButton) {
        display.text = "0"
       
    }

}

'Swift Study > Storyboard' 카테고리의 다른 글

[iOS / Swift] Storyboard 간단한 로또앱 만들기  (0) 2024.12.11
[iOS / Swift] Storyboard 간단한 날씨앱 만들기  (1) 2024.12.11
[iOS / Swift] Storyboard 간단한 로그인 기능 구현  (0) 2024.12.09
[iOS / Swift] Storyboard 간단한 계산기 만들기  (0) 2024.12.04
Swift 팀 소개 앱 만들기  (0) 2024.06.28
'Swift Study/Storyboard' 카테고리의 다른 글
  • [iOS / Swift] Storyboard 간단한 날씨앱 만들기
  • [iOS / Swift] Storyboard 간단한 로그인 기능 구현
  • [iOS / Swift] Storyboard 간단한 계산기 만들기
  • Swift 팀 소개 앱 만들기
이도서
이도서
  • 이도서
    도서의 코딩노트
    이도서
  • 전체
    오늘
    어제
    • 분류 전체보기 (249)
      • Today I Learned (79)
        • 2024 (78)
      • Swift (25)
        • Swift 문법 정리 (19)
        • RxSwift (5)
      • Swift Study (12)
        • Playground (2)
        • Storyboard (9)
        • UIKit (1)
      • Flutter (27)
        • Dart 언어 (16)
        • Flutter Study (11)
      • React (37)
        • HTML & CSS (8)
        • JavaScript 기본 (12)
        • JavaScript 심화 (14)
        • Node.js (2)
        • React (1)
      • Git (3)
      • 코딩테스트 (60)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

    • 개발 공부 공유 링크
  • 인기 글

  • 태그

    javascript 기본
    html
    ios
    storyboard
    Flutter
    function
    iOS앱개발
    React
    CLASS
    객체지향
    코딩테스트Level.1
    DART
    코딩테스트 level.1
    내일배움캠프
    코딩테스트
    Til
    함수
    Swift
    ios앱개발자
    javascript
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
이도서
Swift 계산기 만들기 (UIKit-Stroyboard)
상단으로

티스토리툴바