nslocale

NSLocale preferredLanguages objectAtIndex:0 always return “en”

♀尐吖头ヾ 提交于 2019-11-27 14:28:15
问题 Simulator's language already changed to Japanese, why [[NSLocale preferredLanguages] objectAtIndex:0] always return "en" ? Neither preferred language nor region is related to "en", where does "en" come from? Tried to get [[NSLocale preferredLanguages] objectAtIndex: 2 ] , then crash shows *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (2) beyond bounds (1) Does it means there is no any languages are set in simulator? So weird!

Get country code from country name in IOS

冷暖自知 提交于 2019-11-27 12:44:57
Im retrieving a country name from a server in english. For example, "Spain" What I want to do is, assuming the country name is going to be written in english, get the country code. What should I do? I´ve found getting the country name from the country code quite easy, but I´ve got no idea of how to do the opposite operation. Thanks a lot. Breakpoint Jef's answer helped here, with slight additions. NSArray *countryCodes = [NSLocale ISOCountryCodes]; NSMutableArray *countries = [NSMutableArray arrayWithCapacity:[countryCodes count]]; for (NSString *countryCode in countryCodes) { NSString

ios时间那点事--NSCalendar NSDateComponents

混江龙づ霸主 提交于 2019-11-27 12:05:51
#iOS时间那点事 ##NSCalendar + NSDateComponents 历法能使人类确定每一日再无限的时间中的确切位置并记录历史。 日历,历法,一般历法都是遵循固定的规则的,具有周期性。日历都是已知的或可预测的。 任何一种具体的历法,首先必须明确规定起始点,即开始计算的年代,这叫“纪元”;以及规定一年的开端,这叫“岁首”。此外,还要规定每年所含的日数,如何划分月份,每月有多少天等等。 NSCalendar对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算。 NSDateFomatter 表示的时间默认以公历(即阳历)为参考,可以通过设置calendar属性变量获得特定历法下的时间表示。 NSDate 是独立与任何历法的,它只是时间相对于某个时间点的时间差; NSDate 是进行日历计算的基础。 NSDateComponents将时间表示成适合人类阅读和使用的方式,通过NSDateComponents可以快速而简单地获取某个时间点对应的“年”,“月”,“日”,“时”,“分”,“秒”,“周”等信息。当然一旦涉及了年月日时分秒就要和某个历法绑定,因此NSDateComponents必须和NSCalendar一起使用,默认为公历。 NSDateComponents除了像上面说的表示一个时间点外,还可以表示时间段,例如:两周,三个月,20年,7天

How to get NSNumberFormatter currency style from ISOCurrencyCodes?

限于喜欢 提交于 2019-11-27 07:01:06
问题 If I were to have EUR, or USD (both ISO currency codes), how could I make my NSNumberFormatter to properly format me a string like this? For EUR code: 10.000,00 € For USD code: $10,000.00 I could do this by setting localeIdentifier. But I really need to get it from the ISO currency code. Is this possible? [numberFormatter stringFromNumber: [_property price]]; Thank you! 回答1: Use the setCurrencyCode: method of NSNumberFormatter . [numberFormatter setCurrencyCode:@"EUR"]; or [numberFormatter

How to get current language code with Swift?

左心房为你撑大大i 提交于 2019-11-26 23:58:52
问题 I want get the language code of the device (en, es...) in my app written with Swift. How can get this? I'm trying this: var preferredLanguages : NSLocale! let pre = preferredLanguages.displayNameForKey(NSLocaleIdentifier, value: preferredLanguages) But this returns nil. Thanks! 回答1: In Swift 3 let langStr = Locale.current.languageCode 回答2: It's important to make the difference between the App language and the device locale language (The code bellow is in Swift 3 ) Will return the Device

ios时间那点事--NSLocale

不想你离开。 提交于 2019-11-26 23:34:17
#iOS时间那点事 ##NSLocale 若你只开发中国区的应用,需要保证用户修改当前语言环境时应用的显示不发生变化。而像NSDateFormatter这样的类,会根据设备的设置,自动返回不同语言的数据。为了保证返回数据的语言一致,我们需要设置NSLocale。 下面的代码就可以保证在任何语言环境下,只返回中文的数据: <!-- lang: cpp --> NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"]; NSDateFormatter *secondDateFormatter = [[NSDateFormatter alloc] init]; [secondDateFormatter setDateFormat:@"cccc"]; secondDateFormatter.locale = locale; NSDate *date = [NSDate date]; NSLog(@"%@", [secondDateFormatter stringFromDate:date]); 当然,像上面的需求很罕见。 作为大家都不常用的一个类,NSLocale类是将与国家和语言相关的信息进行简单的组合,包括货币,文学方面的信息。 货币:货币的国际名称(人民币的国际货币名称是CNY);货币符号

how to use nsdateformatter to Venezuela

亡梦爱人 提交于 2019-11-26 18:40:27
问题 I'm trying to schedule a date on the calendar, which has a start time and an end time, everything works fine if the time zone is in the U.S., and the date the change does not help and I need it to Venezuela what should I do as I place generica, to serve me anywhere no matter the time zone? here I leave the code as data and bring me as I make the date format. comes the date in the following format: 12/10/2012 05:00 pm EKEvent* event = [EKEvent eventWithEventStore:eventStore]; NSDateFormatter

Getting current device language in iOS?

社会主义新天地 提交于 2019-11-25 23:19:40
问题 I\'d like to show the current language that the device UI is using. What code would I use? I want this as an NSString in fully spelled out format. (Not @\"en_US\") EDIT: For those driving on by, there are a ton of useful comments here, as the answer has evolved with new iOS releases. 回答1: The solutions provided will actually return the current region of the device - not the currently selected language. These are often one and the same. However, if I am in North America and I set my language