Interview questions
① gives an array that requires swift to write a function that swaps two elements in the array.
//to an array that requires swift to write a function that swaps two elements in the array. 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. Generics//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//Safety Judgment ifA = = B | | A <0|| A > Count-1|| b <0|| B > Count-1 { return} (Nums[a],nums[b])=(Nums[b],nums[a])}
② Circular Reference
// Circular reference (pre-property +weak decoration) 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
③ uses Swift to implement a function, input is any integer, output to return the input integer +2
Simple: Func addTwo (input:int)-Int {returnInput +2 }//What is the curry?//currying refers to the transformation from a multi-parameter function into a series of single-parameter functions.Func Add (Input:int), (INT)Int {return{Valueinch returnInput +Value}} Let AddTwo= Add (Input:2) Let Oupput= AddTwo (8) print (oupput)classBankcard {//Balancevar balance:double =0.0 //How to save moneyfunc Deposit (amount:double) {balance+=Amount Print ("deposit:\ (balance)") } }//Curry?Let card =Bankcard () card.deposit (amount: -) Let deposit=bankcard.depositdeposit (Card) ( -)
④ Simplifying Code
Swift face question related