[文章]iOS字符串处理笔记,包括如何使用正则表达式解析,NSScanner扫描,设置和使用CoreParse解析器来解析处理自定义符号等内容

搜索

在一个字符串中搜索子字符串

  • 最灵活的方法
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale

格式化字符串

  • 3个方法
-initWithFormat:
-initWithFormat:arguments:
+stringWithFormat:

整数

  • 可以同时工作在32位和64位的
uint64_t p = 2305843009213693951;
NSString *s = [NSString stringWithFormat:@"The ninth Mersenne prime is %llu", (unsigned long long) p];
// "The ninth Mersenne prime is 2305843009213693951"
Modifierd, io, u, x, X
hh signed char unsigned char
h short unsigned short
(none) int unsigned int
l(ell) long unsigned long
j intmax_t uintmax_t
t ptrdiff_t  
z   size_t
  • 转换规则
int m = -150004021;
uint n = 150004021U;
NSString *s = [NSString stringWithFormat:@"d:%d i:%i o:%o u:%u x:%x X:%X", m, m, n, n, n, n];
// "d:-150004021 i:-150004021 o:1074160465 u:150004021 x:8f0e135 X:8F0E135"
//o是八进制
  • 设置最小字段宽度和最小数字位数
int m = 42;
NSString *s = [NSString stringWithFormat:@"'%4d' '%-4d' '%+4d' '%4.3d' '%04d'", m, m, m, m, m];
// "[ 42] [42 ] [ +42] [ 042] [0042]"
m = -42;
NSString *s = [NSString stringWithFormat:@"'%4d' '%-4d' '%+4d' '%4.3d' '%04d'", m, m, m, m, m];
// "[ -42] [-42 ] [ -42] [-042] [-042]"
  • %p可打印指针,和%#x不同的是它可以同时在32位和64位执行

浮点数

  • 使用%f和%g
double v[5] = {12345, 12, 0.12, 0.12345678901234, 0.0000012345678901234};
NSString *s = [NSString stringWithFormat:@"%g %g %g %g %g", v[0], v[1], v[2], v[3], v[4]];
// "12345 12 0.12 0.123457 1.23457e-06"
NSString *s = [NSString stringWithFormat:@"%f %f %f %f %f", v[0], v[1], v[2], v[3], v[4]];
// "12345.000000 12.000000 0.120000 0.123457 0.000001"

多行文字

  • 使用 来
NSString *limerick = @"A lively young damsel named Menzies
"
@"Inquired: «Do you know what this thenzies?»
"
@"Her aunt, with a gasp,
"
@"Replied: "It's a wasp,
"
@"And you're holding the end where the stenzies.
";
  • 等价写法
NSString *limerick = @"A lively young damsel named Menzies
Inquired: «Do you know what this thenzies?»
Her aunt, with a gasp,
Replied: "It's a wasp,
And you're holding the end where the stenzies.
";
  • 更简洁的方法
NSString * string = @"The man " @"who knows everything " @"learns nothing" @".";

替换字符串

  • NSMutableString的四个方法
-deleteCharactersInRange:
-insertString:atIndex:
-replaceCharactersInRange:withString:
-replaceOccurrencesOfString:withString:options:range:
  • NSString的方法
-stringByReplacingOccurrencesOfString:withString:
-stringByReplacingOccurrencesOfString:withString:options:range:
-stringByReplacingCharactersInRange:withString:
  • NSMutableString不会创建新字符串,性能会好点
NSMutableString *string; // 假设我们已经有了一个名为 string 的字符串
// 现在要去掉它的一个前缀,做法如下:
NSString *prefix = @"WeDon’tWantThisPrefix"
NSRange r = [string rangeOfString:prefix options:NSAnchoredSearch range:NSMakeRange(0, string.length) locale:nil];
if (r.location != NSNotFound) {
     [string deleteCharactersInRange:r];
}

连接字符串

NSArray *names = @["Hildr", @"Heidrun", @"Gerd", @"Guðrún", @"Freya", @"Nanna", @"Siv", @"Skaði", @"Gróa"];
NSString *result = [names componentsJoinedByString:@", "];

字符串解析

正则表达式

NSError *error = nil;
NSString *pattern = @"(\w+) = #(\p{Hex_Digit}{6})";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:&error];
NSTextCheckingResult *result = [expression firstMatchInString:string
options:0
range:NSMakeRange(0, string.length)];
NSString *key = [string substringWithRange:[result rangeAtIndex:1]];
NSString *value = [string substringWithRange:[result rangeAtIndex:2]];

将字符串分解成数组,使用componentsSeparatedByString:这个方法,或者enumerateSubstringsInRange:options:usingBlock:。如果是按照行来进行分解可以使用option这个参数传NSStringEnumerationByLines

NSString *input = @“
backgroundColor = #ff0000
textColor = #0000ff
"
NSString *pattern = @"(\w+) = #([\da-f]{6})";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:NULL];
NSArray *lines = [input componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (NSString *line in lines) {
     NSTextCheckingResult *textCheckingResult = [expression firstMatchInString:line
          options:0
          range:NSMakeRange(0, line.length)];
     NSString* key = [line substringWithRange:[textCheckingResult rangeAtIndex:1]];
     NSString* value = [line substringWithRange:[textCheckingResult rangeAtIndex:2]];
     result[key] = value;
}
return result;

扫描

  • NSScanner
NSScanner *scanner = [NSScanner scannerWithString:string];
//默认情况下,扫描器会跳过所有空格符和换行符。但这里我们只希望跳过空格符
scanner.charactersToBeSkipped = [NSCharacterSet whitespaceCharacterSet];
//定义一个十六进制字符集
NSCharacterSet *hexadecimalCharacterSet =
[NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];

NSMutableDictionary *result = [NSMutableDictionary dictionary];
while (!scanner.isAtEnd) {
     NSString *key = nil;
     NSString *value = nil;
     NSCharacterSet *letters = [NSCharacterSet letterCharacterSet];
     BOOL didScan = [scanner scanCharactersFromSet:letters intoString:&key] &&
          [scanner scanString:@"=" intoString:NULL] &&
          [scanner scanString:@"#" intoString:NULL] &&
          [scanner scanCharactersFromSet:hexadecimalCharacterSet intoString:&value] &&
     value.length == 6;
     result[key] = value;
     [scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet]
          intoString:NULL]; // 继续扫描下一行
}
return result;

解析器

  • 设计一个能够用(100,0,255)或者#ff0000这样的字符来定义颜色的方法。
- (NSDictionary *)parse:(NSString *)string error:(NSError **)error
{
     self.scanner = [NSScanner scannerWithString:string];
     self.scanner.charactersToBeSkipped = [NSCharacterSet whitespaceCharacterSet];

     NSMutableDictionary *result = [NSMutableDictionary dictionary];
     NSCharacterSet *letters = [NSCharacterSet letterCharacterSet]
     while (!self.scanner.isAtEnd) {
          NSString *key = nil;
          UIColor *value = nil;
          BOOL didScan = [self.scanner scanCharactersFromSet:letters intoString:&key] &&
               [self.scanner scanString:@"=" intoString:NULL] &&
               [self scanColor:&value];
          result[key] = value;
          [self.scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet]
               intoString:NULL]; // 继续扫描下一行
     }
}

- (BOOL)scanColor:(UIColor **)out
{
     return [self scanHexColorIntoColor:out] || [self scanTupleColorIntoColor:out];
}

//扫描设置#ff0000这样的
- (BOOL)scanHexColorIntoColor:(UIColor **)out
{
     NSCharacterSet *hexadecimalCharacterSet =
          [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
     NSString *colorString = NULL;
     if ([self.scanner scanString:@"#" intoString:NULL] &&
          [self.scanner scanCharactersFromSet:hexadecimalCharacterSet
          intoString:&colorString] &&
          colorString.length == 6) {
          *out = [UIColor colorWithHexString:colorString];
          return YES;
     }
     return NO;
}

- (BOOL)scanTupleColorIntoColor:(UIColor **)out
{
     NSInteger red, green, blue = 0;
     BOOL didScan = [self.scanner scanString:@"(" intoString:NULL] &&
          [self.scanner scanInteger:&red] &&
          [self.scanner scanString:@"," intoString:NULL] &&
          [self.scanner scanInteger:&green] &&
          [self.scanner scanString:@"," intoString:NULL] &&
          [self.scanner scanInteger:&blue] &&
          [self.scanner scanString:@")" intoString:NULL];
     if (didScan) {
          *out = [UIColor colorWithRed:(CGFloat)red/255.
               green:(CGFloat)green/255.
               blue:(CGFloat)blue/255.
               alpha:1];
          return YES;
     } else {
          return NO;
     }
}

符号化处理

先进星扫描,使用NSScanner来解析这个表达式

myView.left = otherView.right * 2 + 10
viewController.view.centerX + myConstant <= self.view.centerX
NSScanner *scanner = [NSScanner scannerWithString:contents];
NSMutableArray *tokens = [NSMutableArray array];
while (![scanner isAtEnd]) {
     for (NSString *operator in @[@"=", @"+", @"*", @">=", @"<=", @"."]) {
          if ([scanner scanString:operator intoString:NULL]) {
               [tokens addObject:operator];
          }
     }
}
//接下来识别非符号的只包含字母的string
NSString *result = nil;
if ([scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet]
          intoString:&result]) {
     [tokens addObject:result];
}

//NSScanner有scanDouble:来扫描double
double doubleResult = 0;
if ([scanner scanDouble:&doubleResult]) {
     [tokens addObject:@(doubleResult)];
}
//完成后用将需要解析的表达式放入试试
NSString* example = @"myConstant = 100
"
     @"
myView.left = otherView.right * 2 + 10
"
     @"viewController.view.centerX + myConstant <= self.view.centerX";
NSArray *result = [self.scanner tokenize:example];
NSArray *expected = @[@"myConstant", @"=", @100, @"myView", @".", @"left",
     @"=", @"otherView", @".", @"right", @"*", @2, @"+",
     @10, @"viewController", @".", @"view", @".",
     @"centerX", @"+", @"myConstant", @"<=", @"self",
     @".", @"view", @".", @"centerX"];
XCTAssertEqualObjects(result, expected);

进行语法解析,需要语法分析库描述我们的语言。下面代码就是为那个布局约束语言写的解析语法,用的扩展的巴科斯范式EBNF写法:

constraint = expression comparator expression
comparator = "=" | ">=" | "<="
expression = keyPath "." attribute addMultiplier addConstant
keyPath = identifier | identifier "." keyPath
attribute = "left" | "right" | "top" | "bottom" | "leading" | "trailing" | "width" | "height" | "centerX" | "centerY" | "baseline"
addMultiplier = "*" atom
addConstant = "+" atom
atom = number | identifier

还有很多Objective-C的语法解析,更多的可以在CocoaPods上找到:http://cocoapods.org/?q=parse。比较好的就是CoreParse,地址:https://github.com/beelsebob/CoreParse,但是需要使用它支持的语法。下面就是CoreParse支持的格式:

NSString* grammarString = [@[
     @"Atom ::= num@'Number' | ident@'Identifier';",
     @"Constant ::= name@'Identifier' '=' value@<Atom>;",
     @"Relation ::= '=' | '>=' | '<=';",
     @"Attribute ::= 'left' | 'right' | 'top' | 'bottom' | 'leading' | 'trailing' | 'width' | 'height' | 'centerX' | 'centerY' | 'baseline';",
     @"Multiplier ::= '*' num@'Number';",
     @"AddConstant ::= '+' num@'Number';",
     @"KeypathAndAttribute ::= 'Identifier' '.' <AttributeOrRest>;",
     @"AttributeOrRest ::= att@<Attribute> | 'Identifier' '.' <AttributeOrRest>;",
     @"Expression ::= <KeypathAndAttribute> <Multiplier>? <AddConstant>?;",
     @"LayoutConstraint ::= lhs@<Expression> rel@<Relation> rhs@<Expression>;",
     @"Rule ::= <Atom> | <LayoutConstraint>;",
] componentsJoinedByString:@"
"];

一个规则匹配后解析器就找到同样名称的类

- (id)parser:(CPParser *)parser didProduceSyntaxTree:(CPSyntaxTree *)syntaxTree
     NSString *ruleName = syntaxTree.rule.name;
     if ([ruleName isEqualToString:@"Attribute"]) {
          return self.layoutAttributes[[[syntaxTree childAtIndex:0] keyword]];
     }
...
}

完整的解析器代码在:https://github.com/objcio/issue-9-string-parsing。里面有个解析类可以用来解析复杂的布局约束,如下:

viewController.view.centerX + 20 <= self.view.centerX * 0.5

可以得到如下结果,方便转换成NSLayoutConstraint对象

(<Expression: self.keyPath=(viewController, view),
     self.attribute=9,
     self.multiplier=1,
     self.constant=20>
-1
<Expression: self.keyPath=(self, view),
     self.attribute=9,
     self.multiplier=0.5,
     self.constant=0>)

字符串的渲染

UILabel

  • label默认显示一行,如果设置numberOfLines为大于1的话可以显示指定行数,如果设置为0,则多少行都显示
  • attributedText属性可以显示富文本
  • label的font,textColor,textAlignment,shadowColor和shadowOffset属性可以改变外观。
  • 改变程序内所有Label的风格,可以使用[UILabel appearance]方法

UITextField

  • text field只限于单行
  • UITextfield实现了UITextInputTraits协议,这个协议需要指定键盘外观和操作等细节。比如显示什么键盘和返回按键响应等
  • 可以通过设置左右辅助视图,或者设置背景来自定义输入框风格了。

UITextView

TableView中显示动态文本

Table view的Delegate有个方法用来计算高度:tableView:heightForRowAtIndexPath:。自定义一个UITableViewCell的子类

- (void)layoutSubviews
{
     [super layoutSubviews];
     self.textLabel.frame = CGRectInset(self.bounds,
          MyTableViewCellInset,
          MyTableViewCellInset);
}

计算真实高度需要使用boundingRectWithSize:options:context: 这个方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CGFloat labelWidth = self.tableView.bounds.size.width - MyTableViewCellInset*2;
     NSAttributedString *text = [self attributedBodyTextAtIndexPath:indexPath];
     NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin |
          NSStringDrawingUsesFontLeading;
     CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
          options:options
          context:nil];

     return (CGFloat) (ceil(boundingRect.size.height) + MyTableViewCellInset*2);
}

使用Text Kit和NSAttributedString进行布局

先设置attributes

CGFloat const fontSize = 15;

NSMutableDictionary *body1stAttributes = [NSMutableDictionary dictionary];
body1stAttributes[NSFontAttributeName] = [UIFont fontWithName:@"BodoniSvtyTwoITCTT-Book"
size:fontSize];
NSMutableParagraphStyle *body1stParagraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
body1stParagraph.alignment = NSTextAlignmentJustified;
body1stParagraph.minimumLineHeight = fontSize + 3;
body1stParagraph.maximumLineHeight = body1stParagraph.minimumLineHeight;
body1stParagraph.hyphenationFactor = 0.97;
body1stAttributes[NSParagraphStyleAttributeName] = body1stParag
raph;

这里字体为BodoniSvtyTwoITCTT,如果需要查看更多字体可以使用 +[UIFont familyNames]这个方法。为了得到字体的名字,可以使用 +[UIFont fontNamesForFamilyName:]。接下来创建段落的属性

NSMutableDictionary *bodyAttributes = [body1stAttributes mutableCopy];
NSMutableParagraphStyle *bodyParagraph =
     [bodyAttributes[NSParagraphStyleAttributeName] mutableCopy];
bodyParagraph.firstLineHeadIndent = fontSize;
bodyAttributes[NSParagraphStyleAttributeName] = bodyParagraph;

装饰段落风格,使用装饰字体将文本居中对齐,装饰字符的前后加上空白段落

NSMutableDictionary *ornamentAttributes = [NSMutableDictionary dictionary];
ornamentAttributes[NSFontAttributeName] = [UIFont fontWithName:@"BodoniOrnamentsITCTT"
     size:36];
NSMutableParagraphStyle *ornamentParagraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
ornamentParagraph.alignment = NSTextAlignmentCenter;
ornamentParagraph.paragraphSpacingBefore = fontSize;
ornamentParagraph.paragraphSpacing = fontSize;
ornamentAttributes[NSParagraphStyleAttributeName] = ornamentParagraph;

显示数字表格table,表格布局示例

NSCharacterSet *decimalTerminator = [NSCharacterSet
     characterSetWithCharactersInString:decimalFormatter.decimalSeparator];
NSTextTab *decimalTab = [[NSTextTab alloc]
     initWithTextAlignment:NSTextAlignmentCenter
     location:100
     options:@{NSTabColumnTerminatorsAttributeName:decimalTerminator}];
NSTextTab *percentTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight
     location:200
     options:nil];
NSMutableParagraphStyle *tableParagraphStyle =
     [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
tableParagraphStyle.tabStops = @[decimalTab, percentTab];

显示列表的属性设置如下

NSMutableDictionary *listAttributes = [bodyAttributes mutableCopy];
NSMutableParagraphStyle *listParagraph =
     [listAttributes[NSParagraphStyleAttributeName] mutableCopy];
listParagraph.headIndent = fontSize * 3;
listParagraph.firstLineHeadIndent = fontSize;
NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentNatural
     location:fontSize * 3
     options:nil];
listParagraph.tabStops = @[listTab];
listAttributes[NSParagraphStyleAttributeName] = listParagraph;

字符串本地化

[文章]Core Animation笔记,基本的使用方法

基本动画

  • 使用 CABasicAnimation,实现一个动画
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position.x";
animation.fromValue = @77;
animation.toValue = @455;
animation.duration = 1;

//使rocket留在最终状态,设置removedOnCompletion为No以防止它被自动移除
animation.fillMode = kCAFillModeForward;
animation.removedOnCompletion = NO;

[rocket.layer addAnimation:animation forKey:@"basic"];

多步动画

  • 使用CAKeyframeAnimation,实现一个动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position.x";
animation.values = @[ @0, @10, @-10, @10, @0 ];
animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];
animation.duration = 0.4;

animation.additive = YES;

[form.layer addAnimation:animation forKey:@"shake"];

沿路径的动画

CGRect boundingRect = CGRectMake(-150, -150, 300, 300);

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
orbit.keyPath = @"position";
orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(boundingRect, NULL));
orbit.duration = 4;
orbit.additive = YES;
orbit.repeatCount = HUGE_VALF;
orbit.calculationMode = kCAAnimationPaced;
orbit.rotationMode = kCAAnimationRotateAuto;

[satellite.layer addAnimation:orbit forKey:@"orbit"];

时间函数

CAMediaTimingFunction

  • time function也可以称作easing函数,最简单的easing函数是linear。在Core Animation中这个功能由CAMediaTimingFunction 类表示
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position.x";
animation.fromValue = @50;
animation.toValue = @150;
animation.duration = 1;

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

[rectangle.layer addAnimation:animation forKey:@"basic"];

rectangle.layer.position = CGPointMake(150, 0);

Core Animation附带了一些linear之外的内置easing函数

  • Ease in (kCAMediaTimingFunctionEaseIn): 
  • Ease out (kCAMediaTimingFunctionEaseOut): 
  • Ease in ease out (kCAMediaTimingFunctionEaseInEaseOut): 
  • 默认 (kCAMediaTimingFunctionDefault): 

+functionWithControlPoints:::: 创建自己的 easing 函数

CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position.x";
animation.fromValue = @77;
animation.toValue = @455;
animation.duration = 1;

animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.5:0:0.9:0.7];

[rocket.layer addAnimation:animation forKey:@"basic"];

rocket.layer.position = CGPointMake(150, 0);

动画组

  • 复杂动画,同时为多个属性进行动画。同时对position,rotation和z-position进行动画。使用CAAnimationGroup
CABasicAnimation *zPosition = [CABasicAnimation animation];
zPosition.keyPath = @"zPosition";
zPosition.fromValue = @-1;
zPosition.toValue = @1;
zPosition.duration = 1.2;

CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
rotation.keyPath = @"transform.rotation";
rotation.values = @[ @0, @0.14, @0 ];
rotation.duration = 1.2;
rotation.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];

CAKeyframeAnimation *position = [CAKeyframeAnimation animation];
position.keyPath = @"position";
position.values = @[
[NSValue valueWithCGPoint:CGPointZero],
[NSValue valueWithCGPoint:CGPointMake(110, -20)],
[NSValue valueWithCGPoint:CGPointZero]
];
position.timingFunctions = @[
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
];
position.additive = YES;
position.duration = 1.2;

CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
group.animations = @[ zPosition, rotation, position ];
group.duration = 1.2;
group.beginTime = 0.5;

[card.layer addAnimation:group forKey:@"shuffle"];

card.layer.zPosition = 1;

[文章]Swift的一些使用说明

Optionals

  • swif不允许nil传入,如果要达到能够传nil使用如下方法添加?这个符号标识是
extension NSAttributedString {
     init(string str: String?)
}
  • 返回一个可以为nil的返回值
func parseColorFromHexString(input: String) -> UIColor? {
     // ...
}

值类型与引用类型

  • 值类型赋值和作为参数时都是复制的。值类型包括数字,字符串,数组,字典,枚举,元组和结构体
var a = "Hello"
var b = a
b.extend(", world")
println("a: (a); b: (b)") // a: Hello; b: Hello, world
  • 引用类型可以有多所有者,一般类都是引用类型
var a = UIView()
var b = a
b.alpha = 0.5
println("a: (a.alpha); b: (b.alpha)") // a: 0.5; b: 0.5

函数

标准函数

func hello(name: String, age: Int, location: String) {
     println("Hello (name). I live in (location) too. When is your (age + 1)th birthday?")
}
hello("Mr. Roboto", 5, "San Francisco")

外部参数

  • 外部参数名称解决调用函数时不确定每个函数参数代表什么的问题
func hello(fromName name: String) {
     println("(name) says hello to you!")
}
hello(fromName: "Mr. Roboto")
  • 如需要外部参数和内部参数名相同,只需要在参数前加上#即可
func hello(#name: String) {
     println("hello (name)")
}
hello(name: "Robot")

类中的方法

标准参数

  • 类中的方法参数调用和函数不同,第一个参数不被外部包含,后面的的参数会被作为外部参数名和objective-c一样
class MyFunClass {
     func helloWithName(name: String, age: Int, location: String) {
          println("Hello (name). I live in (location) too. When is your (age + 1)th birthday?")
     }
}
let myFunClass = MyFunClass()
myFunClass.helloWithName("Mr. Roboto", age: 5, location: "San Francisco")
  • 如果不想显示外部参数名可以通过添加一个_解决
class MyFunClass {
     func helloWithName(name: String, _ age: Int, _ location: String) {
          println("Hello (name). I live in (location) too. When is your (age + 1)th birthday?")
     }
}
let myFunClass = MyFunClass()
myFunClass.helloWithName("Mr. Roboto", 5, "San Francisco")

初始化

  • 初始化时第一个参数必须是外部的
struct Celsius {
     var temperatureInCelsius: Double
     init(fromFahrenheit fahrenheit: Double) {
          temperatureInCelsius = (fahrenheit - 32.0) / 1.8
     }
     init(fromKelvin kelvin: Double) {
          temperatureInCelsius = kelvin - 273.15
     }
     init(_ celsius: Double) {
          temperatureInCelsius = celsius
     }
}

let boilingPointOfWater = Celsius(fromFahrenheit: 212.0)
// boilingPointOfWater.temperatureInCelsius 是 100.0

let freezingPointOfWater = Celsius(fromKelvin: 273.15)
// freezingPointOfWater.temperatureInCelsius 是 0.0

let bodyTemperature = Celsius(37.0)
// bodyTemperature.temperatureInCelsius 是 37.0

Optionals参数

  • 传参是Optionals时,需要做拆包,如下
func myFuncWithOptionalType(optionalParameter: String?) {
     if let unwrappedOptional = optionalParameter {
          println("The optional has a value! It's (unwrappedOptional)")
     } else {
          println("The optional is nil!")
     }
}

myFuncWithOptionalType("someString")
// optional has a value! It's someString

myFuncWithOptionalType(nil)
// The optional is nil

默认参数

  • 设置参数默认值,一般都将默认参数放在后面
func hello(name: String = "you") {
     println("hello, (name)")
}

hello(name: "Mr. Roboto")
// hello, Mr. Roboto

hello()
// hello, you

可变参数

  • 可变参数,需要注意的是可变参数是在函数列表的最后一个
func helloWithNames(names: String...) {
     for name in names {
          println("Hello, (name)")
     }
}

// 2 names
helloWithNames("Mr. Robot", "Mr. Potato")
// Hello, Mr. Robot
// Hello, Mr. Potato

// 4 names
helloWithNames("Batman", "Superman", "Wonder Woman", "Catwoman")
// Hello, Batman
// Hello, Superman
// Hello, Wonder Woman
// Hello, Catwoman

inout

  • 使用引用来操纵外部变量
var name1 = "Mr. Potato"
var name2 = "Mr. Roboto"

func nameSwap(inout name1: String, inout name2: String) {
     let oldName1 = name1
     name1 = name2
     name2 = oldName1
}

nameSwap(&name1, &name2)

name1
// Mr. Roboto

name2
// Mr. Potato

泛型参数类型

  • 一个函数中接受多个类型不定的参数。
func valueSwap<T>(inout value1: T, inout value2: T) {
     let oldValue1 = value1
     value1 = value2
     value2 = oldValue1
}

var name1 = "Mr. Potato"
var name2 = "Mr. Roboto"

valueSwap(&name1, &name2)

name1 // Mr. Roboto
name2 // Mr. Potato

var number1 = 2
var number2 = 5

valueSwap(&number1, &number2)

number1 // 5
number2 // 2

多值返回

func findRangeFromNumbers(numbers: Int...) -> (min: Int, max: Int) {

     var min = numbers[0]
     var max = numbers[0]

     for number in numbers {
          if number > max {
               max = number
          }

          if number < min {
               min = number
          }
     }

     return (min, max)
}

findRangeFromNumbers(1, 234, 555, 345, 423)
// (1, 555)

let range = findRangeFromNumbers(1, 234, 555, 345, 423)
println("From numbers: 1, 234, 555, 345, 423. The min is (range.min). The max is (range.max).")
// From numbers: 1, 234, 555, 345, 423. The min is 1. The max is 555.

let (min, max) = findRangeFromNumbers(236, 8, 38, 937, 328)
println("From numbers: 236, 8, 38, 937, 328. The min is (min). The max is (max)")
// From numbers: 236, 8, 38, 937, 328. The min is 8. The max is 937

返回函数

  • 返回一个函数
func myFuncThatReturnsAFunc() -> (Int) -> String {
     return { number in
          return "The lucky number is (number)"
     }
}

let returnedFunction = myFuncThatReturnsAFunc()

returnedFunction(5) // The lucky number is 5
  • 为返回的函数定义一个别名
typealias returnedFunctionType = (Int) -> String

func myFuncThatReturnsAFunc() -> returnedFunctionType {
     return { number in
          return "The lucky number is (number)"
     }
}

let returnedFunction = myFuncThatReturnsAFunc()

returnedFunction(5) // The lucky number is 5

语法参考

The Swift Programming Language》中文版:http://numbbbbb.gitbooks.io/-the-swift-programming-language-/

[文章]Xcode调试之LLDB

断点和调试器交互

help命令

  • help 列出所有命令
  • help 列出某个命令更多细节,例如help print

print

  • print 打印需要查看的变量,例如print totalCount
  • print 还能使用简写prin, pri, p
  • po(print object)可以打印对象的description方法的结果
  • 打印不同格式可以用p/x number打印十六进制,p/t number打印二进制,p/c char打印字符。这里是完整清单https://sourceware.org/gdb/onlinedocs/gdb/Output-Formats.html

expression

  • expression 可以改变一个值,例如expression s
  • expression可以使用e来代替
  • e -p — dataArray 也可以打印对象的description方法的结果,等同于po

流程控制

  • continue会取消暂停,继续执行下去到达下一个断电,LLDB中使用process continue,别名continue,或者使用缩写c
  • step over会执行当前这个函数,然后继续。LLDB中使用thread step-over,next或者缩写n
  • step into指跳进一个函数调试。LLDB中使用thread step in,step或者s
  • step out会继续执行到下一个返回语句,然后再次停止
  • thread return会在当前断点处直接返回出函数,函数剩余部分不会被执行。LLDB中使用thread return NO

断点管理

  • breakpoint list可以看到所有断点,简写br li
  • breakpoint set可以创建断点,缩写br

在LLDB执行C/Objective-C/C++/Swift

  • 除了创建函数,类,block等其它的都可以做到
  • 使用e进行操作,p进行打印显示结果

在调试器中直接更行UI

(lldb) e id $myView = (id)0x7f82b1d01fd0
(lldb) e (void)[$myView setBackgroundColor:[UIColor blueColor]]
(lldb) e (void)[CATransaction flush]

查找Button的target

查看按钮按下后谁会接收到按钮发出的action

(lldb) po [$myButton allTargets]
{(
<MagicEventListener: 0x7fb58bd2e240>
)}
(lldb) po [$myButton actionsForTarget:(id)0x7fb58bd2e240 forControlEvent:0]
<__NSArrayM 0x7fb58bd2aa40>(
_handleTap:
)

观察实例变量变化

想监视vMain变量什么时候被重写了,监视这个地址什么时候被写入

(lldb) p (ptrdiff_t)ivar_getOffset((struct Ivar *)class_getInstanceVariable([MyView class], "vMain"))
(ptrdiff_t) $0 = 8
(lldb) watchpoint set expression -- (int *)$myView + 8
Watchpoint created: Watchpoint 3: addr = 0x7fa554231340 size = 8 state = enabled type = w
new value: 0x0000000000000000

[文章]Markdown 简明手册

标题

# This is an H1
## This is an H2
###### This is an H6

引用块

> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.

> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
id sem consectetuer libero luctus adipiscing.

列表

无序列表

* Red
* Green
* Blue

有序列表

1. Bird
2. McHale
3. Parish

水平线

下面每一行都会得到一个水平线

* * *
***
*****
- - -
---------------------------------------

链接

这是一个 [连接显示文字](http://example.com/ "Title") 行内链接。
[连接显示文字](http://example.net/) 没有title属性。
<http://example.com/> 自动链接

强调

*single asterisks*
_single underscores_
**double asterisks**
__double underscores__

图片

行内图片

![Alt text](/path/to/img.jpg)
![Alt text](/path/to/img.jpg "Optional title")

引用图片

![Alt text][id]

表格

| Left-Aligned | Center Aligned | Right Aligned |
| :------------ |:---------------:| -----:|
| col 3 is | some wordy text | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |

参考资料

在线编辑器

支持Markdown的网站