foundation

1st april dates of 80s failed to parse in iOS 10.0

故事扮演 提交于 2019-12-17 04:07:12
问题 I found that DateFormatter date(from:) method can't parse a couple of specific dates. Method returns nil for the 1st april of 1981-1984 years. Is it a bug of Foundation? What can we do to perform parsing of such dates? Xcode 8.0, iOS SDK 10.0. Here is a screenshot of a short playground example: 回答1: This problem occurs if daylight saving time starts exactly on midnight , as it was the case in Moscow in the years 1981–1984 (see for example Clock Changes in Moscow, Russia (Moskva)). This was

Is the following a safe use of dispatch_set_target_queue()?

℡╲_俬逩灬. 提交于 2019-12-16 18:07:08
问题 What I want to do is create an indirect queue targeting the main queue. dispatch_queue_t myQueue = dispatch_queue_create("com.mydomain.my-main-queue", NULL); dispatch_set_target_queue(myQueue, dispatch_get_main_queue()); My ultimate goal is to use the queue as the underlyingQueue property of an NSOperationQueue, because Apple's documentation clearly states not to use dispatch_get_main_queue(). Though using an indirect queue it technically is following the documentation. The reason for all

How to express current time in words

时间秒杀一切 提交于 2019-12-14 03:29:55
问题 I am able to get current time, Now I want to output it in words. let date = Date() let calendar = Calendar.current let hour = calendar.component(.hour, from: date) let minutes = calendar.component(.minute, from: date) let seconds = calendar.component(.second, from: date) print("hours = \(hour):\(minutes):\(seconds)") Output 10:30 How to get this like - It's half past ten 回答1: As @MwcsMac points out in his answer, the key to solving this is Formatter (once known as NSFormatter ), particularly

NSURLSession 3xx redirects and completion handlers

。_饼干妹妹 提交于 2019-12-14 01:26:24
问题 I have a dataTask + completionHandler approach to downloading data from a web server. So far I have implemented this: let task = session.dataTaskWithURL(url, completionHandler: { (pageData,response,error) in ... ... let code = urlHttpResponse.statusCode switch code { case 200: self.fetchedPages.updateValue(pageData, forKey: pageNumber) case 404: self.fetchedPages.updateValue(nil, forKey: pageNumber) //No data exists for that page default: self.fetchedPages.updateValue(nil, forKey: pageNumber)

How to map HTML to in NSAttributedString in iOS

帅比萌擦擦* 提交于 2019-12-13 19:55:35
问题 Since the NSAttributedString initWithHTML addtion is only availiable on Mac OS but not iOS. 回答1: You should check Oliver Drobnik's NSAttributedString Additions for HTML project on GitHub. It is a response to precisely that problem. See also his blog post about it. 来源: https://stackoverflow.com/questions/4075223/how-to-map-html-to-in-nsattributedstring-in-ios

How to shorten an NSString that might include e.g. Emojis to the maximal length allowed for a HFS+ filename

眉间皱痕 提交于 2019-12-13 00:55:22
问题 Apples documentation says: [...] current file systems such as HFS+ (used by Mac OS X) allow you to create filenames with a 255-character limit [...] symbols may actually take up to the equivalent of nine English characters to store [...] This should be considered when attempting to create longer names. How do I limit the length of a NSString in a way that it is truly shorter than 255 characters, even when it includes symbols that might take more than one character to store? I add my current

iOS and Objective-C: Repeating an event every quarter

对着背影说爱祢 提交于 2019-12-12 12:10:16
问题 I need to have an event repeat every "quarter" (which I assume means moving up 3 months approximately). So I could expect this to move the date (but it doesn't): NSDateComponents *component = [[NSDateComponents alloc] init]; // quarter component.quarter = 1; self.todoStartDate = [[NSCalendar currentCalendar] dateByAddingComponents: component toDate:self.todoStartDate options: 0]; Is there anything wrong with adding a "quarter" to a date? It works fine when adding a day or a week, but not with

Does a protocol exist that determines a type should be initializable with a String?

◇◆丶佛笑我妖孽 提交于 2019-12-12 11:33:32
问题 I have written the following code to assist converting text values from form fields into expected values: protocol StringConvertableValueType { init?(_ string: String) } extension Int: StringConvertableValueType { } extension String: StringConvertableValueType { } extension Double: StringConvertableValueType { } extension Float: StringConvertableValueType { } As you see all of these primitive types support the same initializer by themselves already. However pouring over the documentation I

Get decimal separator of decimal pad keyboard on iOS

三世轮回 提交于 2019-12-12 08:26:56
问题 I am trying to find out which decimal separator is used by the decimal pad keyboard in iOS, so I can convert strings entered by the user to numbers with NumberFormatter and back. As I want to pre-fill the text field with an existing value, I need to have a number formatter that uses the same decimal separator as the decimal pad keyboard. The language that my device is set to (German, Germany) uses a comma as the decimal separator. I have configured iOS to have the German keyboard as the

Copy object at specific index of mutable array to the end of another array

橙三吉。 提交于 2019-12-12 05:59:54
问题 I have a mutable array that has a range of numbers (that are changed dynamically later on if that helps), I grab a random number's index from that array and want to stick it in another array (also mutable). I'm not sure how to grab the object at a certain index and copy it. Here's what I tried to do: [btnRange addObject:@"12"]; [btnRange addObject:@"13"]; [btnRange addObject:@"14"]; [btnRange addObject:@"17"]; [btnRange addObject:@"18"]; [btnRange addObject:@"19"]; //start randomising and