int ival = 1024; int *pi = &ival; // pi points to an int int **ppi = π // ppi points to a pointer to an int
指向指针的引用
1 2 3 4 5
int i = 42; int *p;//p isa pointer to int int *&r = p;// r is a reference to the pointer p r = &i; // r refers to a pointer; assigning &i to r makes p point to i *r = 0; // dereferencing r yields i, the object to which p points; changes i to 0
Exercises Section 2.3.3 Exercise 2.25: Determine the types and values of each of the following variables.
(a) int* ip, &r = ip;
(b) int i, *ip = 0;
(c) int* ip, ip2;
(a) ip:int类型的指针,r:指向int类型指针的引用
(b) i:int类型的变量,ip:int类型的指针
(c)ip:int类型的指针,ip2:int类型的变量
常量限定符(const)
const修饰的对象不可修改
1 2
constint bufSize = 512; // input buffer size bufSize = 512; // error: attempt to write to const object
const修饰的对象必须初始化
1 2 3
constint i = get_size(); // ok: initialized at run time constint j = 42; // ok: initialized at compile time constint k; // error: k is uninitialized const
在多个文件中使用const修饰的对象
1 2 3 4
// file_1.cc defines and initializes a const that is accessible to other files externconstint bufSize = fcn(); // file_1.h externconstint bufSize; // same bufSize as defined in file_1.cc
Exercises Section 2.4 Exercise 2.26: Which of the following are legal? For those that are illegal,
explain why.
(a) const int buf;
(b) int cnt = 0;
(c) const int sz = cnt;
(d) ++cnt; ++sz;
(a):❌ (d):++cnt;(✅)++sz;(❌)
常量引用
1 2 3 4
constint ci = 1024; constint &r1 = ci; // ok: both reference and underlying object are const r1 = 42; // error: r1 is a reference to const int &r2 = ci; // error: non const reference to a const object
常量引用不可修改值
非常量引用不可指向常量对象
1 2 3 4
int i = 42; constint &r1 = i; // we can bind a const int& to a plain int object const int &r2 = 42; // ok: r1 is a reference to const constint &r3 = r1 * 2; // ok: r3 is a reference to const int &r4 = r * 2; // error: r4 is a plain, non const reference
常量引用可以指向 非常量对象、字面量、通用表达式
1 2
double dval = 3.14; constint &ri = dval;
常量引用会自动强制类型转化
1 2
constint temp = dval; // create a temporary const int from the double constint &ri = temp; // bind ri to that temporary
常量指针(pointer to const)
指向常量的指针,指针地址可以修改,但地址存放的值不可以修改
1 2 3 4
constdouble pi = 3.14; // pi is const; its value may not be changed double *ptr = π // error: ptr is a plain pointer constdouble *cptr = π // ok: cptr may point to a double that is const *cptr = 42; // error: cannot assign to *cptr
指向常量的指针地址存放的值不可修改
1 2
double dval = 3.14; // dval is a double; its value can be changed cptr = &dval; // ok: but can't change dval through cptr
指向常量的指针可以指向非常量对象
指针常量(const Pointers)
1 2 3 4
int errNumb = 0; int *const curErr = &errNumb; // curErr will always point to errNumb const double pi = 3.14159; constdouble *const pip = π // pip is a const pointer to a const object
从右往左看:curErr被const修饰,不可修改
1 2 3 4
*pip = 2.72; // error: pip is a pointer to const // if the object to which curErr points (i.e., errNumb) is nonzero if (*curErr) { *curErr = 0; // ok: reset the value of the object to which curErr is bound }
Exercises Section 2.4.2 Exercise 2.27: Which of the following initializations are legal? Explain why.
(a) int i = -1, &r = 0; (b) int const p2 = &i2; (c) const int i = -1, &r = 0; (d) const int *const p3 = &i2; *(e)** const int *p1 = &i2; (f) const int &const r2; (g) const int i2 = i, &r = i;
(a)❌ 引用必须指向一个对象
(f) ❌常量必须初始化
Exercise 2.28: Explain the following definitions. Identify any that are illegal.
(a) int i, *const cp; (b) int *p1, *const p2;
(c) const int ic, &r = ic;
(d) const int *const p3;
(e) const int *p;
原因:常量初始化没有赋值
常量表达式
常量表达式是指能够在编译时确定值
1 2 3 4
constint max_files = 20; // max_files is a constant expression constint limit = max_files + 1; // limit is a constant expression int staff_size = 27; // staff_size is not a constant expression constint sz = get_size(); // sz is not a constant expression
尽管sz是一个常量,但是它的值只有在运行时才能确定,因此sz不是一个常量表达式
constexpr
1 2 3
constexprint mf = 20; // 20 is a constant expression constexprint limit = mf + 1; // mf + 1 is a constant expression constexprint sz = size(); // ok only if size is a constexpr function
constexpr避免了出现上述sz的情况
1 2
constint *p = nullptr; // p is a pointer to a const int constexprint *q = nullptr; // q is a const pointer to int
constexprint *np = nullptr; // np is a constant pointer to int that is null int j = 0; constexprint i = 42; // type of i is const int // i and j must be defined outside any function constexprconstint *p = &i; // p is a constant pointer to the const int i constexprint *p1 = &j; // p1 is a constant pointer to the int j
A reference is not an object. Instead, a reference is just another name for an already existing object.
1 2 3 4 5 6 7 8 9 10
#include<iostream> usingnamespacestd; intmain(){ int ival = 1024; int &refVal = ival; refVal = 512; cout << ival << endl; // 512 int &refVal2; // error: references must be initialized. return0; }
引用必须初始化
一旦初始化,不可以再将引用指向其他对象
1 2 3
int &refVal4 = 10; // error: initializer must be an object double dval = 3.14; int &refVal5 = dval; // error: initializer must be an int object
Exercises Section 2.3.1
Exercise 2.15: Which of the following definitions, if any, are invalid? Why?
(a) int ival = 1.01;
(b) int &rval1 = 1.01;
(c) int &rval2 = ival;
(d) int &rval3;
(a) ✅(b)❌ 初始化赋值必须是int对象
(c) ✅ (d)❌ 引用必须被初始化
指针(Pointers)
1 2
int *ip1, *ip2; // both ip1 and ip2 are pointers to int double dp, *dp2; // dp2 is a pointer to double; dp is a double
与引用不同,指针本身就是一个对象
在指针的生命周期中,可以指向不同的对象
1 2
int ival = 42; int *p = &ival; // p holds the address of ival; p is a pointer to ival
指针存放的值是另一个对象的地址
1 2 3 4 5
double dval; double *pd = &dval; // ok: initializer is the address of a double double *pd2 = pd; // ok: initializer is a pointer to double int *pi = pd; // error: types of pi and pd differ pi = &dval; // error: assigning the address of a double to a pointer to int
指针类型必须和其指向的地址中存放的对象的类型相同
注:& 和 * 有多种含义
&、*可以当做声明使用,比如:
1 2 3 4
int i = 42; int&r=i; //& *followsatypeandispartofadeclaration; r is a *reference*
int *p; // * *follows a type and is part of a declaration; p is a *pointer*
&也可以作取地址符使用:
1
p = &i; // & is used in an expression as the address-of operator.
*可以作取值符号使用:
1 2
*p = i; // * is used in an expression as the dereference operator int &r2 = *p; // & is part of the declaration; * is the dereference operator
使用指针访问一个对象
1 2 3
int ival = 42; int *p = &ival; // p holds the address of ival; p is a pointer to ival cout << *p; // * yields the object to which p points; prints 42
使用指针指向空指针(Null Pointers)
1 2 3 4
int *p1 = nullptr; // equivalent to int *p1 = 0; int *p2 = 0; // directly initializes p2 from the literal constant 0 // must #include cstdlib int *p3 = NULL; // equivalent to int *p3 = 0;
void指针(void* Pointers)
void指针能够指向任意类型的对象
1 2 3 4
double obj = 3.14, *pd = &obj; // ok: void* can hold the address value of any data pointer type void *pv = &obj; // obj can be an object of any type pv = pd; // pv can hold a pointer to any type
Exercise 2.20: What does the following program do?
tell application "System Events" tell process "Terminal" (* 注意:由于环境的差异性,直接复制这段代码到你的脚本编辑器,可能得不到想要的效果 这里一定要输入你上面entire contents后,打印的"button 1"的完整结果 *) click button 1 of window "终端 — -zsh — 80×24" of application process "Terminal" of application "System Events" end tell end tell
#激活 tell application "Terminal" activate #打开键盘偏好设置 do script "open . '/System/Library/PreferencePanes/Keyboard.prefPane'" end tell
tell application "System Events" tell process "系统偏好设置" #点击修饰键 click button "修饰键…" of tab group 1 of window "键盘" -- of application process "System Preferences" of application "System Events" #更改为默认状态 click button "修饰键…" of tab group 1 of window "键盘" -- of application process "System Preferences" of application "System Events" click button "恢复成默认" of sheet 1 of window "键盘" -- of application process "System Preferences" of application "System Events" click button "好" of sheet 1 of window "键盘" -- of application process "System Preferences" of ®application "System Events" end tell end tell
#符号'¬'(option+'L') 用来将语句延续到第二行 display dialog "This is just a test." buttons {"Great", "OK"} ¬ default button "OK" giving up after 3 --result:调出弹窗,默认键是OK,3秒后消失
变量和属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#变量赋值 set myName to "John" copy 22 to myAge
#局部变量 local windowCount local agentName,agentNumber,agentHireDate
#全局变量 global gAgentCount global gStatementDate,gNextAgentNumber
#属性 property defaultClientName : "Mary Smith"
#字符串中引用变量 set aPerson to "GCS" display dialog "Hello " & aPerson & "!"
类型转换
1
set myText to 2 as text
运算符
1
3 * 7 - "1" --result 20
List(数组)
1 2 3 4 5 6
#初始化数组 set myList to {1, "what", 3} --result: {1, "what", 3} set beginning of myList to 0 --首位设置为0 set end of myList to "four" --末位设置为"four" set item 2 of myList to 4 --第二位设置为4 myList --result: {0, 4, "what", 3, "four"}
Record(键值对)
1 2 3 4
#存值 set myFullName to {firstName:"John", lastName:"Chapman"} #取值 set myLastName to lastName of myFullName --result "Chapman"
2.控制语句
considering/ignoring
1 2 3 4 5 6 7 8 9
"Hello Bob" = "HelloBob" --result: false ignoring white space --忽略空格 "Hello Bob" = "HelloBob" --result: true end ignoring
"BOB" = "bob" --result: true considering case --考虑大小写 "BOB" = "bob" --result: false end considering
try-error
1 2 3 4 5
try word 5 of "one two three" on error error "There are not enough words." end try
if
1 2 3 4 5 6 7 8 9
set currentTemp to 10 if currentTemp < 60 then set response to "It's a little chilly today." else if currentTemp > 80 then set response to "It's getting hotter today." else set response to "It's a nice day today." end if display dialog response
repeat-exit
1 2 3 4 5 6 7 8 9 10
set num to 0 repeat -- perform operations if num < 5 then set num to num + 1 else display dialog num exit repeat end if end repeat
repeat (number) times
1 2 3 4 5 6 7 8
set x to 3 set power to 3
set returnVal to x repeat power - 1 times set returnVal to returnVal * x end repeat return returnVal
repeat while(当型循环)
1 2 3 4 5 6 7 8 9
set userNotDone to true repeat while userNotDone set userNotDone to enterDataRecord() end repeat
on enterDataRecord() delay 3 false end enterDataRecord
数值循环
1 2 3 4 5 6
set n to 3 set returnVal to 0 repeat with i from 0 to n set returnVal to returnVal + I end repeat return returnVal
(数组遍历)
1 2 3 4 5 6 7 8 9 10 11
set peopleList to {"Chris", "David", "Sal", "Ben"}
#方法一 repeat with aPerson in peopleList display dialog "Hello " & aPerson & "!" end repeat
#方法二 repeat with i from 1 to (number of items in peopleList) display dialog "Hello " & item i of peopleList & "!" end repeat
String to List
1 2 3 4 5 6 7
set wordList to words in "Where is the hammer?" repeat with currentWord in wordList log currentWord if (contents of currentWord) is equal to "hammer" then display dialog "I found the hammer!" end if end repeat
record遍历
1 2 3 4 5 6 7 8 9 10 11 12
#在文档中没有找到遍历record的方法,不知道是不是不小心遗漏了 #但是在Stack Overflow中看有如下方法,引入了OC中的Foundation库,实现了record的遍历 use framework "Foundation" set testRecord to {a:"aaa", b:"bbb", c:"ccc"}
set objCDictionary to current application's NSDictionary's dictionaryWithDictionary:testRecord set allKeys to objCDictionary's allKeys()
repeat with theKey in allKeys log theKey as text log (objCDictionary's valueForKey:theKey) as text end repeat
3.函数构造
1 2 3 4 5
on greetClient(nameOfClient) display dialog ("Hello " & nameOfClient & "!") end greetClient
greetClient("GCS_DEVELOPER")
4.脚本对象
定义/执行脚本
1 2 3 4 5 6 7 8 9 10 11 12 13
script John property HowManyTimes : 0 to sayHello to someone set HowManyTimes to HowManyTimes + 1 return "Hello " & someone end sayHello end script
tell John to sayHello to "Herb" #John's sayHello to "Jake" --result: "Hello Jake" #sayHello of John to "Jake" --result: "Hello Jake"
初始化脚本
1 2 3 4 5 6 7 8 9 10 11
on makePoint(x, y) script thePoint property xCoordinate:x property yCoordinate:y end script return thePoint end makePoint set myPoint to makePoint(10,20) get xCoordinate of myPoint --result: 10 get yCoordinate of myPoint --result: 20
script Alex on sayHello() return "Hello, " & getName() end sayHello on getName() return "Alex" end getName end script script AlexJunior property parent : Alex on getName() return "Alex Jr" end getName end script -- Sample calls to handlers in the script objects: tell Alex to sayHello() --result: "Hello, Alex" tell AlexJunior to sayHello() --result: "Hello, Alex Jr." tell Alex to getName() --result: "Alex" tell AlexJunior to getName() --result: "Alex Jr"
In addition, app Clip can’t perform background activity, such as doing background networking with URLSession or maintaining Bluetooth connections when the app clip isn’t in use.