nsstring

Date from String using NSDateFormatter regardless 12h-24h setting

回眸只為那壹抹淺笑 提交于 2019-12-05 19:47:43
Today my question is about date formats and strings. My application downloads some strings representing dates from the internet. The date format is always like this: "2010-05-24 at 20:45" I need to convert this string into an NSDate object in order to perform some date manipulations. I tried this code: NSString * dateString = @"2010-05-24 at 20:45" // actually downloaded from the internet NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init]; [myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"]; NSDate * dateFromString = [myDateFormatter dateFromString:dateString]; This seems

Objective-C: Are all string literals always loaded into memory?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 19:00:17
In UIViewController subclasses, I often see: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = NSLocalizedString(@"Home", nil); } return self; } Why not set self.title in -viewDidLoad ? Are all string literals always loaded into memory? String literals are compiled into your executable file - they are not resources. They are kept in the initialized static data section in the executable. So yes, on the most basic level they are in memory whenever the executable is loaded

UILineBreakMode Vs NSLineBreakMode

China☆狼群 提交于 2019-12-05 18:53:44
问题 I see some UIStringDrawing methods have been updated to use NSLineBreakMode instead of UILineBreakMode in iOS 6.0: E.g. - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode How can I check for this to ensure my iOS 5 users and below continue to use UILineBreakMode? 回答1: No checking is necessary. These are just enums and they map to the same values You can see there is no real difference here: UILineBreakMode vs NSLineBreakMode enum

How do I calculate the exact height of text using UIKit?

大憨熊 提交于 2019-12-05 17:49:19
I'm using -[NSString sizeWithFont] to get the text height. The character 't' is clearly taller than 'm', but -sizeWithFont returns the same height for both these strings. Here's the code: UIFont* myFont = [UIFont fontWithName:@"Helvetica" size:1000.0]; NSString* myStr = @"m"; CGSize mySize = [myStr sizeWithFont:myFont]; With 'm' as shown, it returns {834, 1151} . With myStr = @"t" instead, it's {278, 1151} . The smaller width shows up as expected, but not the height. Does some other function wrap the text tightly? I'm ideally looking for something equivalent to Android's Paint.getTextBounds().

Creating a NSArray from a C Array

感情迁移 提交于 2019-12-05 17:39:09
There are many threads about going the opposite way, but I am interested in converting from a primitive C array to a NSArray. The reason for this is that I want to create a NSString from the array contents. To create the NSString I will use: NSArray *array; NSString *stringFromArray = [array componentsJoinedByString:@","]; I am joining the elements of the array by commas because I will later be saving the string as a .csv file. I don't think it matters, but the C array I am dealing with is of type double and size 43. double c_array = new double [43]; Thanks! NSString * stringFromArray = NULL;

Why does NSLog sometimes print out octal for ucode characters?

北城以北 提交于 2019-12-05 16:38:41
问题 I'm running the following code in the viewDidLoad function of a vanilla iPad single view app: /* * Print the string. A lot. */ for (int i = 0; i < 300; i++) { NSLog(@"%d\n", i); NSLog(@"⊢ ⊣ ⊥ ⊻ ⊼ ⊂ ⊃ ⊑ ⊒ \n"); } The output looks like this: 2013-02-04 20:17:49.718 testplay[59585:c07] 228 2013-02-04 20:17:49.718 testplay[59585:c07] ⊢ ⊣ ⊥ ⊻ ⊼ ⊂ ⊃ ⊑ ⊒ 2013-02-04 20:17:49.719 testplay[59585:c07] 229 2013-02-04 20:17:49.719 testplay[59585:c07] ⊢ ⊣ ⊥ ⊻ ⊼ ⊂ ⊃ ⊑ ⊒ 2013-02-04 20:17:49.719 testplay

How do I split a string with special characters into a NSMutableArray

随声附和 提交于 2019-12-05 15:41:56
I'am trying to seperate a string with danish characters into a NSMutableArray. But something is not working. :( My code: NSString *danishString = @"æøå"; NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[danishString length]]; for (int i=0; i < [danishString length]; i++) { NSString *ichar = [NSString stringWithFormat:@"%c", [danishString characterAtIndex:i ]]; [characters addObject:ichar]; } If I do at NSLog on the danishString it works (returns æøå); But if I do a NSLog on the characters (the array) I get some very stange characters - What is wrong? /Morten First of all,

using NSString + stringWithContentsOfFile:usedEncoding:error:

拜拜、爱过 提交于 2019-12-05 15:03:53
问题 I've got problem with use + stringWithContentsOfFile:usedEncoding:error: My problem in usedEncoding:(NSStringEncoding *)enc I don't know how can i set pointer to encoding. If i make it - programm is fail. For example, in similar function we have encoding:(NSStringEncoding)enc - without pointer! I want loading file (file has encoding ISOLatin1) in NSString and use NSString as UTF8String. how can i make it ? thanks. 回答1: NSStringEncoding encoding; NSError* error; NSString* myString = [NSString

iOS App 唤醒另一个App

萝らか妹 提交于 2019-12-05 14:57:50
iOS App 唤醒另一个App 网上也有讲这块的,感觉讲得都不是很好。而且有一些细节根本没有讲清楚。这里重写整理一下相关知识点。 主要内容 URL Scheme 是什么? 项目中关键的配置 注意事项 URL Scheme 是什么? iOS 有个特性就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用。常见的分享到第三方之间的跳转都是基于 Scheme 的。 通过对比网页链接来理解 iOS 上的 URL Schemes ,应该就容易多了。 URL ,我们都很清楚, http://www.apple.com 就是个 URL ,我们也叫它链接或网址; Schemes ,表示的是一个 UR L 中的一个位置——最初始的位置,即 :// 之前的那段字符。比如 http://www.apple.com 这个网址的 Schemes 是 http 。 根据我们上面对 URL Schemes 的使用,我们可以很轻易地理解,在以本地应用为主的 iOS 上,我们可以像定位一个网页一样,用一种特殊的 URL 来定位一个应用甚至应用里某个具体的功能。而定位这个应用的,就应该这个应用的 URL 的 Schemes 部分,也就是开头儿那部分。比如短信,就是 sms: 你可以完全按照理解一个网页的 URL ——也就是它的网址——的方式来理解一个

Get Unicode point of NSString and put that into another NSString

好久不见. 提交于 2019-12-05 14:12:29
What's the easiest way to get the Unicode value from an NSString ? For example, NSString *str = "A"; NSString *hex; Now, I want to set the value of hex to the Unicode value of str (i.e. 0041)... How would I go about doing that? The unichar type is defined to be a 16-bit unicode value (eg, as indirectly documented in the description of the %C specifier ), and you can get a unichar from a given position in an NSString using characterAtIndex: , or use getCharacters:range: if you want to fill a C array of unichars from the NSString more quickly than by querying them one by one.