Swift面試題相關

來源:互聯網
上載者:User

標籤:sel   init   epo   http   數組   個數   code   var   相關   

面試題

①給一個數組,要求用swift寫一個函數,交換數組中的兩個元素。

    //給一個數組,要求用swift寫一個函數,交換數組中的兩個元素。    var nums = [1,2,3,4,5,6,7]    //        func swap(_ nums: inout [Int], _ a : Int, _ b : Int) {    //            let temp = nums[a]    //            nums[a] = nums[b]    //            nums[b] = temp    //        }        //2.泛型    //        func swap<T>(_ nums: inout [T], _ a : Int, _ b : Int) {    //            let temp = nums[a]    //            nums[a] = nums[b]    //            nums[b] = temp    //        }    //    func swap<T>(_ nums: inout [T], _ a : Int, _ b : Int) {        let count = nums.count        //安全判斷        if a == b || a < 0 || a > count - 1 || b < 0 || b > count - 1   {            return        }        (nums[a],nums[b]) = (nums[b],nums[a])    }

②循環參考

//循環參考(屬性前+weak修飾)class Node {    var value = 0    weak var prev : Node?    weak var next : Node?    init(_ value : Int) {        self.value = value    }    deinit {        print("deinit")    }}let a = Node(0) let b = Node(1)        a.prev = bb.next = a

③使用 swift實現一個函數,輸入是任一整數,輸出要返回輸入的整數+2

簡單:func addTwo(input : Int) -> Int {        return input + 2    }//什麼是柯裡化? //柯裡化指的是從一個多參數函數變成一連串單參數函數的變換    func add (input : Int) -> (Int) -> Int {        return { value in            return input + value        }    }let addTwo = add(input: 2)let oupPut = addTwo(8)print(oupPut)class BankCard {        //餘額        var balance : Double = 0.0        //存錢方法        func deposit(amount: Double) {            balance += amount            print("deposit:\(balance)")        }            }//柯裡化?let card = BankCard()card.deposit(amount: 100)        let deposit = BankCard.depositdeposit(card)(100)

④ 簡化代碼

 

 

Swift面試題相關

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.