nsstring

Why is this code not recognising the NSString as being equal?

梦想的初衷 提交于 2019-11-27 05:39:26
This is the code I have: NSLog(@"name: %@", name); NSLog(@"service: %@", service.name); if (name == service.name) { NSLog(@"Test"); } Name is "Andrew’s MacBook Pro". Service is "Andrew’s MacBook Pro" And yet I don't get a "Test" from NSLog. Any ideas why this could be? Adil Soomro use [string isEqualToString:@"any string"] See a very useful discussion here: Understanding NSString comparison For string comparisons, use [name isEqualToString:service.name] Using == will compare to see if both pointers point to the same object, not if they point to objects with the same contents. Even if both

How to get the first N words from a NSString in Objective-C?

好久不见. 提交于 2019-11-27 05:31:18
问题 What's the simplest way, given a string: NSString *str = @"Some really really long string is here and I just want the first 10 words, for example"; to result in an NSString with the first N (e.g., 10) words? EDIT: I'd also like to make sure it doesn't fail if the str is shorter than N. 回答1: If the words are space-separated: NSInteger nWords = 10; NSRange wordRange = NSMakeRange(0, nWords); NSArray *firstWords = [[str componentsSeparatedByString:@" "] subarrayWithRange:wordRange]; if you want

UIImagePickerDelegate - 官方文档说明

喜你入骨 提交于 2019-11-27 05:23:29
- ( void )imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info   // 当用户选中了一个静态图片或者视频时,会自动发送给委托对象。   info的键值选项如下: 1 NSString *const UIImagePickerControllerMediaType; 2 NSString *const UIImagePickerControllerOriginalImage; 3 NSString *const UIImagePickerControllerEditedImage; 4 NSString *const UIImagePickerControllerCropRect; 5 NSString *const UIImagePickerControllerMediaURL; 6 NSString *const UIImagePickerControllerReferenceURL; 7 NSString *const UIImagePickerControllerMediaMetadata;   你的委托对象在该方法的实现中,传递给需要特定media的对象

align text using drawInRect:withAttributes:

百般思念 提交于 2019-11-27 05:07:14
问题 In the iOS 5 version of my app I had: [self.text drawInRect: stringRect withFont: [UIFont fontWithName: @"Courier" size: kCellFontSize] lineBreakMode: NSLineBreakByTruncatingTail alignment: NSTextAlignmentRight]; I'm upgrading for iOS 7. The above method is deprecated. I'm now using drawInRect:withAttributes: . The attributes parameter is an NSDictionary object. I can get drawInRect:withAttributes: to work for the former font parameter using this: UIFont *font = [UIFont fontWithName: @

Make a two-digit string from a single-digit integer

 ̄綄美尐妖づ 提交于 2019-11-27 05:02:30
问题 How can I have a two-digit integer in a a string, even if the integer is less than 10? [NSString stringWithFormat:@"%d", 1] //should be @"01" 回答1: I believe that the stringWithFormat specifiers are the standard IEEE printf specifiers. Have you tried [NSString stringWithFormat:@"%02d", 1]; 回答2: Use the format string %02d . This specifies to format an integer with a minimum field-width of 2 characters and to pad the formatted values with 0 to meet that width. See man fprintf for all the gory

Capitalize or change case of an NSString in Objective-C

≡放荡痞女 提交于 2019-11-27 04:59:52
问题 I was wondering how to capitalize a string found in an object in an NSMutableArray . An NSArray contains the string 'April' at index 2. I want this to be changed to 'APRIL' . Is there something simple like this? viewNoteDateMonth.text = [[displayDate objectAtIndex:2] capitalized]; 回答1: Here ya go: viewNoteDateMonth.text = [[displayDate objectAtIndex:2] uppercaseString]; Btw: "april" is lowercase ➔ [NSString lowercaseString] "APRIL" is UPPERCASE ➔ [NSString uppercaseString] "April May" is

Replacing in NSString with wildcards / regular expressions - cocoa

狂风中的少年 提交于 2019-11-27 04:57:24
问题 NSString *x=@"\"/Sagar\' and \'Samir\' "; Now, I want to remove characters between these. \" / \' Intended output x = (should have) @"and \'Samir\'" So, Ms word give some options in find & replace, using wild card characters. ( just giving example ) Is it possible in cocoa? 回答1: I'll assume you're looking for a solution in general -- not for this particular string. You'll want to learn about regular expressions. For regular expression support in Objective-C check out RegexKitLite. It provides

Using NSRegularExpression to extract URLs on the iPhone

倖福魔咒の 提交于 2019-11-27 04:39:47
I'm using the following code on my iPhone app, taken from here to extract all URLs from striped .html code. I'm only being able to extract the first URL, but I need an array containing all URLs. My NSArray isn't returning NSStrings for each URL, but the objects descriptions only. How do I make my arrayOfAllMatches return all URLs, as NSStrings? -(NSArray *)stripOutHttp:(NSString *)httpLine { // Setup an NSError object to catch any failures NSError *error = NULL; // create the NSRegularExpression object and initialize it with a pattern // the pattern will match any http or https url, with

objective-c code to right pad a NSString?

白昼怎懂夜的黑 提交于 2019-11-27 04:33:50
问题 Can someone give a code example of how to right pad an NSString in objective-c please? For example want these strings: Testing 123 Long String Hello World Short if right padded to a column width of say 12: and then a sting "XXX" is added to the end of each, it would give: Testing 123 xxx Hello World xxx Short xxx That is a 2nd column would like up. 回答1: Adam is on the right track, but not quite there. You do want to use +stringWithFormat:, but not quite as he suggested. If you want to pad

Number of Occurrences of a Character in NSString

断了今生、忘了曾经 提交于 2019-11-27 04:28:39
I have an NSString or NSMutableString and would like to get the number of occurrences of a particular character. I need to do this for quite a few characters -- uppercase English characters in this case -- so it would be nice for it to be quick. CynicismRising replaceOccurrencesOfString:withString:options:range: will return the number of characters replaced in a NSMutableString . [string replaceOccurrencesOfString:@"A" withString:@"B" options:NSLiteralSearch range:NSMakeRange(0, [receiver length])]; gbaor You can do this in one line. For example, this counts the number of spaces: NSUInteger