博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift5 vs C/C++ (未完待续)
阅读量:4298 次
发布时间:2019-05-27

本文共 4541 字,大约阅读时间需要 15 分钟。

对比一下两种语言的语法,以防止自己搞混。以便于自己记忆。

肯定是不全的,随时增删改查。

 

在线文档;   

 

概念 swift5 C++
打印 print( "Hello World") printf("Hello World");

关键字,

数据类型

var let

enum :  Optional

struct :  Bool、Int、Float、Double、Character,                     String、Array、Dictionary、Set

class : 

 

Void

 

Int8、Int16、Int32、Int64、UInt8、UInt16、UInt32、UInt64

 

asm auto bool break case

catch char class const const_cast
Continue default delete do double
dynamic_cast else enum explicit export
extern false float for friend
goto if inline int long
mutable namespace new operator private
protected public register reinterpret_cast return
short signed sizeof static static_cast
struct switch template this throw
true try typedef typeid typename
union unsigned using virtual void
volatile wchar_t while

变量

var value = 10     //整形  自动识别类型

print(value)

 

var value = "hello world"  //字符串

print(value)

 

var value = [1,3,5,7,9]  //数组

print(value)

 

var dic = ["length":1, "width":2,  "height":3] //字典

print(dic)

 

var stu = ("0001","man",90,"高一9班",170,120)

print(stu)    //元组

int value = 10;

float value = 10.0

 

//字符串

const char *st = "The expense of spirit\n";

 

int ia[10];   //数组

 

int ia[] = { 0, 1, 2 }; //数组

常量

let value = 10    

print(value)

 

let value: Float = 10.5     //指定类型

print(value)

// 缓冲区大小

const int bufSize = 512

if-else let age = 30
if age > 100 {
    print("died")
}else if age > 60 {
    print("old")
}else if age >= 30 {
    print("married")
}else{
    print("happy")
}
    int age = 30;
    if (age > 100) {
        printf("died");
    }else if( age > 60) {
        printf("old");
    }else if( age >= 30 ){
        printf("married");
    }else{
        printf("happy");
    }
while

var num = 3

while num > 0 {
    num = num - 1
    print("hello")
}

 

//repeat-while

var num = 3

repeat{
    num = num - 1
    print("hello")
} while num > 0 

    int num = 3;

    while (num > 0) {
        num = num - 1;
        printf("hello");
    }

 

/do-while

    int num = 3;

    do{
        num = num - 1;
        printf("hello");
    } while( num > 0 );

for

//i 默认是let , 也可以声明为var

for i in 1...5{

    print(i)
}

 

for i in 1...5 where i != 3 {

    print(i)
}

 

let names = ["hello","world","look"]

for name in names[0...2] {
    print(name)
}

    for(int i = 1; i <=5; ++i){
        printf("%d",i);
    }
switch    
函数

func getPi() -> Double {

    return 3.14
}

func sum(v1 : Int,  v2:  Int) -> Int{

    return v1+v2
}

print(getPi())

print(sum(v1 : 3,  v2:  5))

double getPi()

{
    return 3.14;
}

int sum( int v1,  int v2) 

{
    return v1+v2;
}

printf("%.6f",getPi());

printf("%d",sum(3, 5));

输入输出参数

func swapValues(_ v1: inout Int,  _ v2: inout Int) {

    (v1,v2) = (v2,v1)
}

var v1 = 3

var v2 = 4
swapValues(&v1,&v2)
print("v1: \(v1)  v2: \(v2)")

通过 指针 或者 引用
枚举 enum Direction : Int{
    case north
    case south
    case east
    case west
}
print(Direction.north)
print(Direction.north.rawValue)

enum Direction{

     north,
     south,
     east,
     west
};

 

    printf("%d",north);

    printf("%d",south);

获取占用的字节大小

// 8, 分配占用的空间大小

print(MemoryLayout<Int>.stride)

 

// 8, 实际用到的空间大小

print(MemoryLayout<Int>.size)

 

// 8, 对齐参数

print(MemoryLayout<Int>.alignment)

 

 

var ret = "helloworld"

print(MemoryLayout.stride(ofValue: ret))
 

//4

printf("%d",sizeof(int));

 

 

可选项    
struct

struct Date{

    var year:Int 
    var month:Int
    var day:Int
}

var date = Date(year:2019, month:11, day:21)

print(date)

 

//

struct Date{

    var year:Int
    var month:Int
    var day:Int
    
    init(year: Int, month: Int, day: Int){
        self.year = year 
        self.month = month
        self.day =  day
    }
}

var date = Date(year:2017, month:11, day:21)

print(date)

 

 
class

class Size{

    var width = 10
    var height = 20
    func showSize(){
        print("width = \(width),height=\(height)")
    }
}

var size = Size()

size.showSize()

 
闭包表达式    

存储属性

计算属性

struct Rect{

    var width: Int          //存储属性
    
    var area: Int {         //计算属性
        set{
            width = newValue/2
        }
        get{
            width*width
        }
    
    }

}

var rect = Rect(width:5)

print(rect.width)    //5
print(rect.area)     //25

rect.area = 20

print(rect.width)    //10

 
类型属性

struct Car{

    static var count: Int = 0

    init(){

        Car.count = Car.count + 1
    }
}

var car1 = Car()

var car2 = Car()
var car3 = Car()

print(Car.count)

 
继承

//值类型(枚举、结构体)不支持继承,只有类支持继承

class Animal {

    var age = 1
}
class Dog : Animal {
    var weight = 2
}
class ErHa : Dog {
    var iq = 3
}

var erha = ErHa()

print(erha.age)
print(erha.weight)
print(erha.iq)

 
重写实例方法

class Animal{

    func call(){
        print("---animal call---")
    }
}

class Cat : Animal{

    override func call(){
        super.call()
        print("---Cat call---")
    }
}

var animal = Animal()

animal.call()

animal = Cat()

animal.call()

 
协议(Protocol)    

Any

is

protocol Runnable { func run() }

class Person{}

class Student : Person, Runnable {

    func run(){

        print("student run")
    }
    
    func study(){
        print("student study")
    }
}

var stu : Any = 10

print(stu is Int)      //true
stu = "jack"
print(stu is String)     //true
stu = Student()
print(stu is Person)     //true
print(stu is Student)     //true
print(stu is Runnable)     //true

 

 
局部作用域

do{

    //....

}

 
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

 

 

 

 

 

转载地址:http://kanws.baihongyu.com/

你可能感兴趣的文章
Oauth2方式实现单点登录
查看>>
CountDownLatch源码解析加流程图详解--AQS类注释翻译
查看>>
ES相关度评分
查看>>
我们一起做一个可以商用的springboot脚手架
查看>>
idea在搭建ssm框架时mybatis整合问题 无法找到mapper
查看>>
java设计基本原则----单一职责原则
查看>>
HashMap的实现
查看>>
互斥锁 synchronized分析
查看>>
java等待-通知机制 synchronized和waity()的使用实践
查看>>
win10 Docke安装mysql8.0
查看>>
docker 启动已经停止的容器
查看>>
order by 排序原理及性能优化
查看>>
Lock重入锁
查看>>
docker安装 rabbitMq
查看>>
git 常用命令 入门
查看>>
linux安装docker
查看>>
关闭selinx nginx无法使用代理
查看>>
shell 脚本部署项目
查看>>
spring cloud zuul网关上传大文件
查看>>
springboot+mybatis日志显示SQL
查看>>