nsstring

iOS archive(归档)的总结

情到浓时终转凉″ 提交于 2019-12-06 21:52:50
iOS 归档的记录 归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存(实际上是一种文件保存的形式),浏览网上的一些资料后,并结合自己的一些经验,总结成此文。 一、使用archiveRootObject进行简单的归档 使用NSKeyedArichiver进行归档、NSKeyedUnarchiver进行接档,这种方式会在写入、读出数据之前对数据进行序列化、反序列化操作。 归档: [cpp] view plain copy NSString *homeDictionary = NSHomeDirectory(); //获取根目录 NSString *homePath = [homeDictionary stringByAppendingPathComponent:@ "atany.archiver" ]; //添加储存的文件名 BOOL flag = [NSKeyedArchiver archiveRootObject:@”归档” toFile:homePath]; //归档一个字符串 这种方式可以对字符串、数字等进行归档,当然也可以对NSArray与NSDictionary进行归档。返回值Flag标志着是否归档成功,YES为成功,NO为失败。 接档: [cpp] view plain copy [NSKeyedUnarchiver

Find the index of a character in a string

匆匆过客 提交于 2019-12-06 21:26:06
问题 I have a string NSString *Original=@"88) 12-sep-2012"; or Original=@"8) blablabla"; I want to print only the characters before the ")" so how to find the index of the character ")". or how could i do it? Thanks in advance. 回答1: To print the characters before the first right paren, you can do this: NSString *str = [[yourString componentsSeparatedByString:@")"] objectAtIndex:0]; NSLog(@"%@", str); // If you need the character index: NSUInteger index = str.length; 回答2: U can find index of the

iOS 如何解决'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell

馋奶兔 提交于 2019-12-06 20:24:32
解决 reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cellIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 代码如下: NSString *identifier = @"cellIdentifier" ; static BOOL nibri = NO ; if (!nibri) { NSLog ( @"321" ); UINib *nib = [ UINib nibWithNibName : @"CollectionViewCell" bundle : nil ]; [ self . collectionView registerNib :nib forCellWithReuseIdentifier :identifier]; nibri = YES ; } CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier :identifier forIndexPath :indexPath]

Escaping the @“ ” characters in Objective C

冷暖自知 提交于 2019-12-06 19:49:39
I'm trying to set the background image of a view based on a passed "artistID." Here is my code so far: NSString *backgroundImageName = [[NSString alloc]initWithFormat:@"artistbackground%i.png",_artistID]; self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:backgroundImageName]]; NSLog(@"%@",backgroundImageName); unfortunately, for the parameter in ImageNamed, I'm passing in: artistibackground1 instead of: @"artistbackgound1" Any ideas on how to escape the @ and the quotes?? Thanks!! self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:

NSString to float…what happens if NSString is not numerical and contains alphanumeric characters?

我只是一个虾纸丫 提交于 2019-12-06 19:23:25
if NSString sample = @"1sa34hjh#@"; Float 64 floatsample = [sample floatValue]; what happens? what does floatsample contain? Read the documentation . Return Value The floating-point value of the receiver’s text as a float, skipping whitespace at the beginning of the string. Returns HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Also returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number. The best way to figure out the return value is to check the return value yourself. You can create a small program and save it as a file with a .m extension.

stringByAppendingFormat not working

好久不见. 提交于 2019-12-06 19:01:17
问题 I have an NSString and fail to apply the following statement: NSString *myString = @"some text"; [myString stringByAppendingFormat:@"some text = %d", 3]; no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString. any clues most welcome. 回答1: I would suggest correcting to (documentation): NSString *myString = @"some text"; myString = [myString stringByAppendingFormat:@" = %d", 3]; From the docs: Returns a string made by appending to

runtime应用场景

ⅰ亾dé卋堺 提交于 2019-12-06 17:57:26
一、runtime简介 RunTime简称运行时。OC就是 运行时机制 ,也就是在运行时候的一些机制,其中最主要的是消息机制。对于C语言, 函数的调用在编译的时候会决定调用哪个函数 。对于OC的函数,属于 动态调用过程 ,在编译的时候并不能决定真正调用哪个函数,只有在真正运行的时候才会根据函数的名称找到对应的函数来调用。事实证明: 在编译阶段,OC可以 调用任何函数 ,即使这个函数并未实现,只要声明过就不会报错。在编译阶段,C语言调用 未实现的函数 就会报错。 二、runtime作用 1.发送消息 方法调用的本质 ,就是让对象发送消息。objc_msgSend,只有对象才能发送消息,因此以objc开头.使用 消息机制 前提,必须导入#import 消息机制简单使用 // 创建person对象 Person *p = [[Person alloc] init]; // 调用对象方法 [p eat]; // 本质:让对象发送消息 objc_msgSend(p, @selector(eat)); // 调用类方法的方式:两种 // 第一种通过类名调用 [Person eat]; // 第二种通过类对象调用 [[Person class] eat]; // 用类名调用类方法,底层会自动把类名转换成类对象调用 // 本质:让类对象发送消息 objc_msgSend([Person class

Why is this program faster in Python than Objective-C?

蓝咒 提交于 2019-12-06 17:07:46
问题 I got interested in this small example of an algorithm in Python for looping through a large word list. I am writing a few "tools" that will allow my to slice a Objective-C string or array in a similar fashion as Python. Specifically, this elegant solution caught my attention for executing very quickly and it uses a string slice as a key element of the algorithm. Try and solve this without a slice! I have reproduced my local version using the Moby word list below. You can use /usr/share/dict

iPhone dev: Replace uppercase characters in NSString with space and downcase

五迷三道 提交于 2019-12-06 17:00:21
问题 I have: NSString *promise = @"thereAreOtherWorldsThanThese"; which I'm trying to transform into the string: @"There are other worlds than these" I'm guessing this is a regex job, but I'm very new to Objective C, and have so far had no luck. I would greatly appreciate any help! 回答1: I'd use GTMRegex (http://code.google.com/p/google-toolbox-for-mac/), for example: NSString *promise = @"thereAreOtherWorldsThanThese"; GTMRegex *regex = [GTMRegex regexWithPattern:@"([A-Z])"]; NSLog(@"%@", [[regex

how to convert this string to json or dictionary in ios

北战南征 提交于 2019-12-06 16:22:33
问题 I tired with links but those are not working in my case. Can anyone suggest me on how to convert this string to json or dictionary. This string is having a dictionary with an array of strings. NSString *temp=@"{\n name = {\n dob = \"\";\n age = \"61\";\n family = (\n {\n location = location;\n mobile = mobile;\n }\n );\n };\n}"; Here family is an array. 回答1: I got the solution. First I tried your code NSString *temp=@"{\n name = {\n dob = \"\";\n age = \"61\";\n family = (\n {\n location =