Swift learning functions and simple creation of controls

Source: Internet
Author: User

today or repeat yesterday do things--knock code, but the only difference is that the knowledge is different, we further to the deep level of learning, feeling more and more interesting, although near the end of watching everyone's enthusiasm is getting lower, but I know I can not, I have to pay more than others, More practical to do, because I know that my foundation is inferior to others, the goal and motivation and they are different, look at everyone said is just to find a job, and some say as long as every month enough to spend on the good, bear is really everyone here to learn the ultimate purpose, if so, really not as easy as to find a job will just a moment, Also than this day to do this much better, but also paid so expensive costs, so why, since the choice of this road, since the beginning of the dream, so big hope to come here, embarked on this road, then we will unswervingly go on, a better tomorrow only can create, but also only their own to be responsible for themselves, The world does not sell regret medicine, if wait until the end has been settled to regret, which still have what use, so here look resolutely choose the program Road Friends (whether it is ios,.net,java ... Or what, do not give up, to learn to be responsible for themselves, care about your people responsible, now I want to myself and people who care about me: refueling!!!!!!

   Today, we will explain the knowledge we have learned today, as well as some of my self-taught knowledge, I hope that we can make common progress and work together, the following will be attached to the code for your reference, learning to advise, etc.:

  I. Learning of functions

  1. Function structure

In the swift language, the basic structure of the function is this, the concrete structure is as follows:

Function

Func function name (parameter list), (return value type) {

code block to execute

}

   2. Basic examples of functions

Func SayHello (name:string) (String    ) {return"Hello" +   (    String) {return"" + name + name1}

   The above function named SayHello (name:string, name1:string) corresponds to the parameter list, (String) refers to the function of the return value type, the inside of the curly braces is the function to execute the code block, I believe you see the function has a most preliminary understanding of it , and I'm going to attach the little exercises we've done today.

varLetter ="aeibcd1234"func getnumbers (letter:string)-(int, int, int) {varCount:int =0varCount1:int =0varCount2:int =0 forOnecharinchLetter {//var str = String (Onechar). LowerCaseString    SwitchOnechar { Case "a","I","b": Count++;  Case "e","C","D": Count1++; default: Count2++; }}    return(count, Count1, Count2)}getnumbers (letter)

   First you need to define the string, (obviously these are different from OC and UI) var letter = "aeibcd1234", then define three number of shaping to count, I believe the following switch,case everyone can understand it, this is simply branching structure, I use this to judge how many ' a ', ' I ', ' B ' and ' E ', ' C ', ' d ', and how many numbers have been represented by the count (Count1,count2) each time, and, under the instructions, the sentence I commented out is for the case conversion, If there is a string inside the defined string, we can use this method to make it lowercase, so that we count on it later, so that this simple function is here, if there is wrong or where not understand can be put forward oh, hey.

3. Use of external parameters

There are times when we need to introduce an external parameter to achieve our needs, but it is important to note that although there is time to use external parameters to make the programs we write look stunning, external parameters are not allowed to be used inside the function, for example, what we would normally write is similar to the following:

But there is time you will find that we have encountered in the phone or the source code will be different from the above example, such as:

Next the partner door will certainly have a doubt it (know to low-key ha) then I come slowly for you to explain the next, in fact, the implementation is not too difficult, only need a line of code can be done, a total of three kinds of plans, the following one by one for everyone to attach the code for your reference!

We enclose the source code as a comparison

Func Personinfou (personname:string, sex:string,  age:string), String {    return"  " + sex + Age } "

Programme one:

Func Personinfou (name personname:string, sex sex:string, age age:string), String {    return " " + sex + Age } "

is to add an external parameter name in front of each parameter, presumably you can see through the comparison.

Scenario Two:

Func Personinfou (#personName: String, #sex: String, #age: String), string {    return " + sex + Age } "

   Programme III:

"  - ") String {    return" + sex + Age } "

The above is the implementation of the three methods, is not very magical, to say the truth, I did not know before I can write, but now know, it can be said to get a new skill.

4. Nesting of functions

But really, the nesting of functions is very similar to the C-language function callbacks, However, since there are no. h files and. m files in Swift, the developer made a few tweaks to ensure the security of the file's source code, which is to write the nested function body inside the same function, or to act as a code block, to show you the following function nesting in Swift:

//function type as return value//hide don't want others to seeFunc ChangeValue (isone:bool), IntInt {func plusone (number:int)-Int {returnNumber +1; } func Plustwo (number:int)-Int {returnNumber-1; }    returnIsone?Plusone:plustwo}varCc:int-Int = ChangeValue (true) cc ( -)

   In the above code, the parameter list is (Isone:bool) He is a Bool type, passing the following return isone? Plusone:plustwo to the callback of the function, if the argument is true, it starts to callback a function, the second callback, the function is called in the last line below, var cc:int, Int = ChangeValue (True) Pass in a true when return a function body through return, namely PlusOne then call to function cc, pass in parameter 20, execute function, this is the function of simple nesting, as for complex this blog will keep updating, please expect.

   Ii. simple learning of closures in Swift

1. Simple structure of closures

{

(parameter list), return value in

Implementation of closures

}

In fact, Swift in the closure of the block similar to OC, the following for you to attach the simple application of closures, do not miss Oh, layer by step, oh, hey.

(1) The first method of realization

var names = ["qiaoming" "kan""Taobao  "]var sortedNames1: () = sort (&names, {    in    return Stringa < stringb}) names

In fact, there is nothing to say, just apply the structure, but the following Bo friends door to see clearly, is closely linked, one after the other evolved.

(2) The second way of realization

var sortedNames2: () = sort (&names, {    in    return Stringa <  STRINGB}) Names

Here to remove the type let the system to infer the type of STRINGA,STRINGB, compared to the previous one is not touched a little bit of simple and quick, but not the effect of Kazakhstan, the following Ultimate version is coming soon.

(3) The Third Way of realization   

var sortedName3: () =  sort (&names,{    return $0 > $1 })

Is it easier to look at it, and here we use the number of a to represent the first argument of the second argument.

(4) Fourth Way of realization

var sortedName4: () = sort (&names, >) names

This is the fourth way of realization, slowly looking at seemingly out of the block, but only in a word can achieve our previous requirements (that is, to sort an array), it should be noted that these are also limited, in the swift language, because the array must be stored in the same data type, So the parameter type can omit to write, and there are names in each method just to verify whether it satisfies our needs, in fact, can not write at all, of course, we write more time.

Third, class and structural body

1. The same point

(1) Both can define the property (2) can be defined method (3) They can all define the convenience constructor (initialization method) (4) can be followed by Protocol (5) can be extended

2. Different points

(1) classes can be inherited, and structs cannot (2) classes can be inferred by type (3) classes can be freed by a destructor (that is, Dealloc) class It is a reference type struct is a value type

The above is what we are learning today is only a point, although not much, but is really new knowledge, feel very useful, after all, is just out of the day will be sent to use, so still want to try to learn it well. The following is a gift oh (oneself through spare time to learn how to create a simple control with Swift), the following for you to attach the code for everyone to refer to the study:

Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?)- >Bool {Self.window=UIWindow (Frame:UIScreen.mainScreen (). Bounds)//Override point for customization after application launch.self.window!. BackgroundColor =Uicolor.whitecolor () Self.window!. makekeyandvisible ()//creation of LebelLet Textlabel = UILabel (Frame:cgrectmake (self.window!. Frame.size.width/2- Max, $, -, -)) Textlabel.text=""Textlabel.backgroundcolor= Uicolor (red:0.5, Green:0.5, Blue:0.5, Alpha:1.0) Textlabel.textcolor= Uicolor (red:0.5, Green:0.2, Blue:0.6, Alpha:1.0) Textlabel.textalignment=Nstextalignment.center Self.window!. Addsubview (Textlabel)//creating and adding images of UiimageviewLet ImageView = Uiimageview (Frame:cgrectmake (self.window!. Frame.size.width/2- Max, -, -, -)) Imageview.backgroundcolor= Uicolor (red:0.5, Green:0.5, Blue:0.5, Alpha:1.0) Let image= UIImage (named:"1.png") Imageview.image=Image Self.window!. Addsubview (ImageView)return true

I believe you will look at these and the UI in the creation of the method is completely different, and will feel not very good, but it is true, when I write this time I feel not quite right, also a bit strange, unexpectedly is so written, but eventually came out, feel very happy, Hope that these can help everyone, if the simple control of the creation of any doubt, welcome to come to discuss with me, by the way, this is in the empty template written, language no longer try to select the OC, and to be rewritten as swift, haha.

Well, the first to write here today, I feel that although things are not many, but still need to slowly digest, after all, is just contact, and finally to repeat what I have just begun to say, chose this path of friends and family, and you you, we work together to uncover our beautiful tomorrow, adhere to the victory, Not to mention a few months have come, the rest of us will be afraid of it, refueling refueling, come on!

Swift learning functions and simple creation of controls

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.