nsstring

Search through NSArray for string

一世执手 提交于 2019-11-28 17:05:38
I would like to search through my NSArray for a certain string. Example: NSArray has the objects: "dog", "cat", "fat dog", "thing", "another thing", "heck here's another thing" I want to search for the word "another" and put the results into one array, and have the other, non results, into another array that can be filtered further. Ken Aspeslagh Not tested so might have a syntax error, but you'll get the idea. NSArray* inputArray = [NSArray arrayWithObjects:@"dog", @"cat", @"fat dog", @"thing", @"another thing", @"heck here's another thing", nil]; NSMutableArray* containsAnother =

How to see if an NSString starts with a certain other string?

落花浮王杯 提交于 2019-11-28 16:54:47
问题 I am trying to check to see if a string that I am going to use as URL starts with http. The way I am trying to check right now doesn't seem to be working. Here is my code: NSMutableString *temp = [[NSMutableString alloc] initWithString:@"http://"]; if ([businessWebsite rangeOfString:@"http"].location == NSNotFound){ NSString *temp2 = [[NSString alloc] init]; temp2 = businessWebsite; [temp appendString:temp2]; businessWebsite = temp2; NSLog(@"Updated BusinessWebsite is: %@", businessWebsite);

How to convert from int to string in objective c: example code

社会主义新天地 提交于 2019-11-28 16:47:29
I am trying to convert from an int to a string but I am having trouble. I followed the execution through the debugger and the string 'myT' gets the value of 'sum' but the 'if' statement does not work correctly if the 'sum' is 10,11,12. Should I not be using a primitive int type to store the number? Also, both methods I tried (see commented-out code) fail to follow the true path of the 'if' statement. Thanks! int x = [my1 intValue]; int y = [my2 intValue]; int sum = x+y; //myT = [NSString stringWithFormat:@"%d", sum]; myT = [[NSNumber numberWithInt:sum] stringValue]; if(myT==@"10" || myT==@"11"

Resulting lines of UILabel with UILineBreakModeWordWrap

让人想犯罪 __ 提交于 2019-11-28 16:34:50
I have a UILabel whose size is calculated with sizeWithFont: method. The line break mode is set to UILineBreakModeWordWrap (same flag is used when calculating the size with sizeWithFont: )... Everything works great, label is properly sized and displays my text as required. Now I need to know the lines that are used to display the label (or the lines that are generated when sizeWithFont: is used). I could technically write my own implementation of line breaking based on spaces/caret returns, but then it's not going to be guaranteed the same way as Apple's implementation and hence the resulting

Check if NSString instance is contained in an NSArray

只谈情不闲聊 提交于 2019-11-28 15:54:46
问题 I have an array with a bunch of strings and I want to check if a certain string is contained in the array. If I use the containsObject : message on the array, I'm getting correct results. Do all NSString objects with the same string point to the same object? Or why is the containsObject : working? NSArray *stringArray = [NSArray arrayWithObjects:@"1",@"2",@"3",anotherStringValue, nil]; if([stringArray containsObject:@"2"]){ //DO SOMETHING } 回答1: Yes, hard-coded NSStrings (string literals)

String search in string array in objective c

早过忘川 提交于 2019-11-28 15:51:23
I want to search a specific string in the array of strings in objective c. Can somebody help me in this regard? JeremyP BOOL isTheObjectThere = [myArray containsObject: @"my string"]; or if you need to know where it is NSUInteger indexOfTheObject = [myArray indexOfObject: @"my string"]; I strongly recommend you read the documentation on NSArray . It's best to do that before posting your question :-) You can use NSPredicate class for searching strings in array of strings. See the below sample code. NSMutableArray *cars = [NSMutableArray arrayWithObjects:@"Maruthi",@"Hyundai", @"Ford", @"Benz",

Case insensitive comparison NSString

冷暖自知 提交于 2019-11-28 15:40:46
Can anyone point me to any resources about case insensitive comparison in Objective C? It doesn't seem to have an equivalent method to str1.equalsIgnoreCase(str2) Jason Coco if( [@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame ) { // strings are equal except for possibly case } The documentation is located at Search and Comparison Methods NSString *stringA; NSString *stringB; if (stringA && [stringA caseInsensitiveCompare:stringB] == NSOrderedSame) { // match } Note: stringA && is required because when stringA is nil : stringA = nil; [stringA caseInsensitiveCompare

ios开发之NSData

末鹿安然 提交于 2019-11-28 15:22:13
NSData用于保存字节数组。 初始化 - (instancetype)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; 1 初始化对象。 不进行复制字节数组操作,直接设置字节指针为bytes,长度为length。 - (instancetype)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; 1 初始化对象。 不进行复制字节数组操作,直接设置字节指针为bytes,长度为length。 - (instancetype)initWithBytes:(nullable const void *)bytes length:(NSUInteger)length; 1 初始化对象。 复制字节数组,设置字节指针指向复制的字节数组,长度为length。 - (nullable instancetype)initWithContentsOfFile:(NSString *)path; 1 读取文件内容初始化对象。 读取成功则返回对象,如果失败则返回nil。 - (nullable instancetype)initWithContentsOfFile:(NSString *)path options:

UITableView 系列之自定义 UITableViewCell

左心房为你撑大大i 提交于 2019-11-28 14:58:12
本来不打算写UITableView的,因为网上已经有很多这方面的文章,尤其是 趣味苹果开发中的TableViewController系列 已经有很详细的讲解了。但是群里还是有很多童鞋会问这方面的问题,所以在这里以自己的理解方式比划一下。 让我们先来看一张从模拟器截下来的图: 上图是一个UITableView列表,红色的1、2、3、4、5...是一个个的UITableViewCell。 从这张截图我们可以看出来 UITableView 是由一系列 UITableViewCell 组成的列表,由此我们可以知道 UITableViewCell 在 UITableVeiw 中的重要性了。 在真实地项目中,UITableViewCell 中的各项内容的排列都不同(如上图4中的 2013年、全国等),它自带的那几种样式根本无法满足我们的需求,所以这就需要我们来自定义自己的Cell(下文中的Cell都表示UITableViewCell)了。 以上图中的Cell为例,我们来自定义一个UITableViewCell。首先我们来创建一个应用——CustomTableVeiwCellDemo, 打开XCode,选择File -> New -> Project...,如下图所示: 然后选择iOS->Application->Single View Application,然后点Next,如下图所示:

Passing NSString value between classes

∥☆過路亽.° 提交于 2019-11-28 14:49:58
I have been looking long for a solution how to pass the value from one class to another. Basically I create a NSString via @property in a Settings class, which is displayed modally. I then set the NSString to a certain value depending on the settings chosen, and I want to have the value shown also in a class where the settings are supposed to make change. I also declare a string via @property in the second class, and I use the code myController *controller = [[myController alloc] init]; secondClassString = controller.firstClassString If I NSLog the string, it shows (null) in the second class..