Swift语言学习No.1

25 篇文章 0 订阅

最近开始学习iOS的游戏开发,正好看到一个系列的教程(https://www.bloc.io/tutorials/swiftris-build-your-first-ios-game-with-swift#!/chapters/675),还用的是Swift,于是就决定开始学习用swift开发。

我这系列blog就依照在swift的学习过程中碰到的问题,然后做一个记录和总计,以备后续复习。

先上程序吧(PS,本文中所有的code都源自开篇所讲到的blog里面)

super.init(size: size)

     anchorPoint = CGPoint(x: 0, y: 1.0)

     let background = SKSpriteNode(imageNamed: "background")
     background.position = CGPoint(x: 0, y: 0)
     background.anchorPoint = CGPoint(x: 0, y: 1.0)
     addChild(background)

1. Let和Var

接触到了swift的第一个关键字 let,万能的stackoverflow上面有个回答讲述的非常全面:

The: let keyword defines a constant:

let theAnswer = 42

The theAnswer cannot be changed afterwards.

The var defines an ordinary variable:

What is interesting:

The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.

Another strange feature:

You can use almost any character you like for constant and variable names, including Unicode characters:

通俗的讲,let定义的是一个常亮,而var定义的是一个变量

同时这个答案更是举了个浅显易懂的例子:

Both var and let are references, therefore let is a const reference. Using fundamental types doesn't really show how let is different than const. The difference comes when using it with class instances (reference types):

class CTest
{
    var str : String = ""
}

let letTest = CTest()
letTest.str = "test" // OK

letTest.str = "another test" // Still OK

//letTest = CTest() // Error

2. anchorPoint

虽然我还没有完全理解透彻anchorPoint的含义,教程中的这段话简单的介绍了一下:
SpriteKit is based on OpenGL and therefore its coordinate system is opposite to iOS' native cocoa coordinates. 
0, 0 in SpriteKit is the bottom-left corner. 
Swiftris will be drawn from the top down so therefore we anchor our game in the top-left corner of the screen: 0, 1.0
We then create an SKSpriteNode capable of representing our background image and we add it to the scene.

但是从0,0是左下角而0,1是左上角可以得出anchorPoint其实是个百分比。而在苹果的官方文档中指出:
anchorPoint  Property

Defines the point in the sprite that corresponds to the node’s position.

Declaration

SWIFT

var anchorPoint: CGPoint

OBJECTIVE-C

@property(nonatomic) CGPoint anchorPoint

Discussion

You specify the value for this property in the unit coordinate space. The default value is (0.5,0.5), which means that the sprite is centered on its position.

Import Statement

import SpriteKit

Availability

Available in iOS 7.0 and later.




























这样看来,anchorPoint并不是swift专有的。

3. ?和!

虽然语法看起来很奇怪,不过swift倒也符合人们的直观感觉,这样的代码
var scene: GameScene!
表示GameScence不是一个optional的变量,什么是optional,就是这个变量可以是nil的,也可以不是。
Swift typically enforces instantiation either in-line where you declare the variable or during the initializer, init…. In order to circumvent this requirement we've added an ! after the type.

Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值,也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化。如果在使用变量之前不进行初始化就会报错:


var stringValue : String 
//error: variable 'stringValue' used before being initialized
//let hashValue = stringValue.hashValue
//                            ^
let hashValue = stringValue.hashValue



更多详细的解释直接看这个帖子吧:http://www.ruanman.net/swift/learn/4569.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swift 是编写程序的绝佳选择,无论是手机、电脑还是服务器,任何能跑代码的设备都是如 此。它是一门集现代语言之大成,集结了苹果的工程师文化精髓以及开源社区多样化于一身的 编程语言。编译器为专为性能所调优,语言专为开发所优化,二者绝不互相妥协。 Swift 语言还对新的程序员十分友好。它是第一个工业级系统编程语言,却又像脚本语言那样 富有张力且十分有趣。在 Playground 编写代码并实时查看 Swift 代码运算结果,完全不需要 从头编译然后运行软件。 Swift 通过向其他现代编程模式学习,定义了大量类来避免常⻅的编程错误: 变量一定是在使用前初始化的; 数组索引会检查越界错误; 整数会检查溢出; 可选项保证了 nil 值会显式处理; 内存自动管理; 错误处理允许从意外错误中恢复控制。 Swift 代码为大部分现代硬件编译和优化。语法和基本库都基于指导原则设计,这显然也是你 让代码的最佳方式。这使得集安全和速度于一身的 Swift 适用于任何场景,从编写 “Hello, world!”到整个操作系统,都是上上之选。 Swift 用轻量级的语法集合强大的类型接口和模式匹配,能够把复杂的想法以简洁优雅的形式 表达。得益于此,代码不仅仅更好写了,还变得更加易读和益于优化。 Swift 已经投放市场多年,它还会继续演进,带来更多的新特性和新功能。我们对 Swift 语言充 满了信心,我们期待着你用 Swift 进行创作!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值