Generics and exception handling in Swift language

Source: Internet
Author: User

I feel the generics are hard, and I hope it helps.

A generic, B-value exchange, which types can be passed to

Func myswap<t> (inout a:t,inout b:t) {

Let temp = a

A = b

b = Temp

}

var a = 111

var B = 222

Myswap (&a, B: &b)

Ordinary Shaping A, B value exchange, can only be passed into the shaping

Func mySwap2 (inout a:int,inout b:int) {

Let temp = a

A = b

b = Temp

}

var z = "AA"

var y = "BB"

MYSWAP2 (&z, B: &y) Here is an error because it specifies that it must be shaped to be exchanged.

swift< generics > Simulation implementation into the stack

struct Stack<element> {

var containers = [Element] ()

mutating func push (e:element) {

Containers.append (e)

//    }

Mutating func pop ()->element {

Return Containers.removelast ()

//    }

//}

//

var data:stack<int> = stack<int> ()

Data.push (1)

Data.push (2)

Data.push (3)

Data.pop ()

Join us to extend a generic, we just need to write the generic name, do not write <> parameters inside

Extension Stack {

var Conut:int {

Return Containers.count

//    }

//}

//

var data2 = stack<string> ()

//

Data2.push ("Adas")

Data2.push ("DASDASD")

Data2.conut

Generic constraints

The normal calculation of the subscript function

Func index (arr: [Int],data:int)->int {

For (A, B) in Arr.enumerate () {

If b = = Data {

Return a

//        }

//    }

Return-1

//}

//

Let array = [1,4,5,6,3]

Index (array, data:3)

How generics are implemented

Generics are not allowed to use = = must inherit this protocol of the system equatable

Protocol ia{

}

Protocol ib{

}

Extension int:ia,ib{

}

Func index<t:equatable where t:ia,t:ib> (arr:[t],data:t)->int {

for (M,n) in Arr.enumerate () {

if n = = data {

return m

}

}

Return-1

}

Let array2 = [1,3,8,2]

Index (ARRAY2,DATA:8)

Class Person {

var salary = 0

}

Func calc<t:person> (Persons:[t]), Int {

var total = 0

For p in persons {

Total + = P.salary

}

Return Total

}

The association type of the agreement

The association type of the protocol (associative type)

Protocol Containter {

Typealias Element

mutating func push (e:element)

Mutating func pop () Element

}

struct mystack:containter{

Typealias Element = Int

var containers = [Int] ()

mutating func push (E:int) {

Containers.append (e)

}

Mutating func pop (), Int {

Return Containers.removelast ()

}

}

struct Stack<element>: containter {

According to the current implementation, the following code can be omitted because of the type inference

Typealias Element = E

var containers = [Element] ()

mutating func push (e:element) {

Containers.append (e)

}

Mutating func pop (), Element {

Return Containers.removelast ()

}

}

Typealias MyInt = int//Take an individual name for int

Let I:myint = 3

Typealias s = String

Let ss:s = "121212"

Constraints when extending the agreement and the Use of the Protocol association type

Protocol MM {

}

Class Mmclass:mm {

}

Protocol nn:mm{

}

Class Nnclass:nn {

}

Protocol Container {

Typealias ItemType

}

Extension Container where itemtype:mm {

var b:int {return 5}

}

Extension Container where Itemtype:nn {

var b:int {return 6}

}

Class Testtest:container {

Typealias ItemType = mmclass//If this is changed to Mmclass, then the AAAA.B output structure is 5

}

Class Testtest2:container {

Typealias ItemType = Int

}

Let AAAA = TestTest2 ()

AAAA.B Error

Swift modifiers are internal by default

Internal class Testmodifier {

Func m () {

}

}

Let Tu = (Testmodifier (), "AA")

Exception Swfit exception handling there are four ways

Creating your own exceptions is also an error class

Enum Passworderror:errortype {

Case Lengthisnotenough

Case Toosimple

}

The first kind of throw out

Func validatepwd (pwd:string) throws-Bool {

Let count1 = Pwd.characters.count

If Count1 < 6 {

Throw Passworderror.lengthisnotenough

}

If Count1 > 10 {

Throw Passworderror.toosimple

}

return True

}

The second kind of solution

do {

Can write multiple lines of code

Try Validatepwd ("DASDASD")

Can write multiple lines of code

}catch {

Print ("error")

}

Third Kind

Let result = Try?validatepwd ("Dadada")

if result = = Nil {

Print ("Error on Call")

}

Fourth type

Let result2 = try! Validatepwd ("Dasdhua")

Defer is a bit like the finally in exception handling, but he can also be used in non-exception handling situations

Func m () {

Print ("Hello")

Defer {

Print ("Good for You")

}

Print ("Good for Everyone")

}

M ()

Generics and exception handling in Swift language

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.