Swift - 基本数据类型,及常/变量声明

本文介绍了Swift语言的基础概念,包括常量、变量、字符串操作、元组及随机数生成等基本语法,并通过创建UILabel实例演示了字体设置的方法。
 1 extension UIFont {
 2     class func fontName(fontName: String, size fontSize: CGFloat) -> UIFont {
 3         
 4         return UIFont(name: fontName, size: fontSize)!
 5     }
 6 }
 7 class ViewController: UIViewController {
 8 
 9     override func viewDidLoad() {
10         super.viewDidLoad()
11         // Do any additional setup after loading the view, typically from a nib.
12         self.navigationItem.title = "Swift"
13         
14         ///1.常量
15         let number: Int = 10
16         print("the number is \(number)")
17         //1.1变量
18         var string :String = "hello world!"
19         print("\(string.count)个字符")
20         //1.2检查字符串是否有特定前缀/后缀:hasPrefix/hasSuffix
21         if string.hasPrefix("hello") {
22             print("hello首部")
23         }
24         if string.hasSuffix("!"){
25             print("!在尾部")
26         }
27         //1.3空字符串
28         string = "";
29         if string.isEmpty {
30             print("空字符串")
31         }else{
32             print("非空字符")
33         }
34         //1.4 元组
35         let myproject = (one:"game",two:12345)
36         print("myproject:\(myproject)")
37         
38         //1.3 获取随机数
39         let temp:Int = Int(arc4random()%100) + 1;
40         print(temp)
41         let temps : Int = Int(arc4random_uniform(100)) + 1;
42         print(temps)
43         
44         /// 创建label
45         let label = UILabel.init(frame: CGRect.init(x: 10, y: 15 + TOP_HEIGHT, width: SCREEN_WIDTH - 20, height: 30))
46         label.text = "the number is \(number)"
47         label.backgroundColor = RED_COLOR;
48         label.textColor = GREEN_COLOR
49         label.font = UIFont.fontName(fontName: "PingFangSC-Regular", size: 14)
50         view.addSubview(label)
51         var variableNumber = 42
52         variableNumber = 24
53         print("the variableNumber is \(variableNumber)")
54         
55         
56         
57     }

 

转载于:https://www.cnblogs.com/edensyd/p/9405394.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值