Swift face question related

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.