• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS10ProgrammingFundamentalswithSwift学习笔记0

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1,a single statement can be broken into multiple lines ,For example, after an opening parenthesis is a good place:

print(

  "world")

2,Swift is a compiled language, the syntax of message-sending is dot-notation. every noun is an object, and every verb is a message.

3,An object type can be extended in Swift, meaning that you can define your own messages on that type. For example, you can’t normally send the say- Hello message to a number. But you can change a number type so that you can:

    extension Int {
        func sayHello() {
            print("Hello, I'm \(self)")
        }
    }
    1.sayHello() // outputs: "Hello, I'm 1"

4, what “everything is an object” really means. ?

In Swift, then, 1 is an object. In some languages, such as Objective-C, it clearly is not; it is a “primitive” or scalar built-in data type. So the distinction being drawn here is between object types on the one hand and scalars on the other. In Swift, there are no scalars; all types are ultimately object types. That’s what “everything is an object” really means. 

Swift has three kinds of object type: classes, structs, and enums. 

5,Variables : A variable is a name for an object. Technically, it refers to an object; 

let one =1

var two = 2

one = two //compile error 

The two kinds of variable declaration differ in that a name declared with let cannot have its value replaced. A variable declared with let is a constant; its value is assigned once and stays.  

Variables literally have a life of their own — more accurately, a lifetime of their own. As long as a variable exists, it keeps its value alive. 

6,Functions : A function is a batch of code that can be told, as a batch, to run.  

7,A namespace is a named region of a program.  

8,The top-level namespaces are modules. 

9.  self.   an instance needs a way of sending a message to itself. This is made possible by the magic word self. 

It turns out that every use of the word self I’ve just illustrated is completely optional. You can omit it and all the same things will happen 。

The reason is that if you omit the message recipient and the message you’re sending can be sent to self, the compiler supplies self as the message’s recipient under the hood. 

10,private:  Supose I want a Dog instance itself to be able to change self.whatADogSays. Then whatADogSays has to be a var; otherwise, even the instance itself can’t change it. Also, suppose I don’t want any other object to know what this Dog says, except by calling bark or speak. Even when declared with let, other objects can still read the value of whatADogSays. Maybe I don’t like that. To solve this problem, Swift provides the private keyword. 

11,If you’re ignoring a function call result deliberately, you can silence the compiler warning by assigning the function call to _ (a variable without a name) — for example, _ = sum(4,5). Alternatively, if the function being called is your own, you can prevent the warning by marking the function declaration with @discardableResult. 

12,Function Signature :(Int, Int) -> Int 。A function’s signature is, in effect, its type — the type of the function.  The signature of a function must include both the parameter list (without parameter names) and the return type, even if one or both of those is empty;  

13,

func echo(string s:String, times n:Int) -> String {
        var result = ""
        for _ in 1...n { result += s}
        return result
    }

In the body of that function, there is now no times variable available; times is purely an external name, for use in the call. The internal name is n, and that’s the name the code refers to.  

14, Default Parameter Values : To specify a default value in a function declaration, append = and the default value after the parameter type:

    class Dog {
        func say(_ s:String, times:Int = 1) {
            for _ in 1...times {
                print(s)
}}

15, To indicate in a function declaration that a parameter is variadic, follow it by three dots, like this:

    func sayStrings(_ arrayOfStrings:String ...) {
        for s in arrayOfStrings { print(s) }
}

 

16, The default separator: (for when you provide multiple values) is a space, and the default terminator: is a newline; you can change either or both: 

    print("Manny", "Moe", separator:", ", terminator:", ")
    print("Jack")
    // output is "Manny, Moe, Jack" on one line

17, Modi able Parameters ,In the body of a function, a parameter is essentially a local variable. By default, it’s a variable implicitly declared with let.

You can’t assign to it 

 func say(_ s:String, times:Int, loudly:Bool) {
        loudly = true // compile error

If your code needs to assign to a parameter name within the body of a function, declare a var local variable inside the function body and assign the parameter value to it; 

  func removeCharacter(_ c:Character, from s:String) -> Int {

        var s = s
        var howMany = 0
        while let ix = s.characters.index(of:c) {
s.remove(at:ix)
howMany += 1 }
        return howMany
    }

 this change didn’t affect the original string 

If we want our function to alter the original value of an argument passed to it, we must do the following:

  • The type of the parameter we intend to modify must be declared inout.

  • When we call the function, the variable holding the value we intend to tell it to

    modify must be declared with var, not let.

  • Instead of passing the variable as an argument, we must pass its address. This is

    done by preceding its name with an ampersand (&). Our removeCharacter(_:from:) now looks like this:

    1.     func removeCharacter(_ c:Character, from s: inout String) -> Int {
              var howMany = 0
      
              while let ix = s.characters.index(of:c) {
                  s.remove(at:ix)
      

      howMany += 1 }

              return howMany
          }
      
 18,You may encounter variations on this pattern when you’re using Cocoa. The Cocoa APIs are written in C and Objective-C, so you probably won’t see the Swift term inout. You’ll probably see some mysterious type such as UnsafeMutablePointer.  
 
let c = UIColor.purple
    var r : CGFloat = 0
    var g : CGFloat = 0
    var b : CGFloat = 0
    var a : CGFloat = 0
    c.getRed(&r, green: &g, blue: &b, alpha: &a)
    func popoverPresentationController(
        _ popoverPresentationController: UIPopoverPresentationController,
        willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
        in view: AutoreleasingUnsafeMutablePointer<UIView>) {
            view.pointee = self.button2
            rect.pointee = self.button2.bounds

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
iOS8CoreImageInSwift:视频实时滤镜发布时间:2022-07-13
下一篇:
Swift语法 →声明与输出发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap