nsstring

How do I save an NSString as a .txt file on my apps local documents directory?

99封情书 提交于 2019-11-30 03:08:24
How do I save an NSString as a .txt file on my apps local documents directory (UTF-8)? NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory NSError *error; BOOL succeed = [myString writeToFile:[documentsDirectory stringByAppendingPathComponent:@"myfile.txt"] atomically:YES encoding:NSUTF8StringEncoding error:&error]; if (!succeed){ // Handle error here } Something like this: NSString *homeDirectory; homeDirectory = NSHomeDirectory(); // Get app's home directory - you

iPhone SDK NSString To NSDate

人盡茶涼 提交于 2019-11-30 03:04:07
问题 I got a string from parsing a XML file which looks like this: Fri, 09 Apr 2010 00:00:45 +0200 and the corresponding pattern should be this "EEE, dd MMM yyyy HH:mm:ss ZZ", but I get (null). This is my code: NSString *dateString = @"Fri, 09 Apr 2010 00:00:45 +0200"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"]; NSDate *date = [dateFormatter dateFromString:dateString]; NSLog(@"date:%@",date); // result date:(null)

Swift string count() vs NSString .length not equal

橙三吉。 提交于 2019-11-30 02:45:50
问题 Why do these two lines give me different results? var str = "Hello 😘" // the square is an emoji count(str) // returns 7 (str as NSString).length // returns 8 Original for reference: 回答1: This is because Swift uses Extended Grapheme Clusters. Swift sees the smiley as one character, but the NSString method sees it as two Unicode Characters, although they are "combined" and represent a single symbol. 回答2: I think the documentation says it best: The character count returned by the count(_:)

Does NSStringFromClass([MyEntityClass class]) generate a safe Core Data Entity name?

萝らか妹 提交于 2019-11-30 02:42:11
问题 Most (all I've seen) Core Data tutorials use the following code snippet with @"MyEntityClass" hard-coded in: NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MyEntityClass"]; Is it safe to use NSStringFromClass() as an Entity Name? NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([MyEntityClass class])]; This seams to be much easer to deal with regarding refactoring and the like. Especially since I am having Xcode create my

How to stop a time UILabel from resizing at every time increment?

前提是你 提交于 2019-11-30 02:40:41
问题 I have a stopwatch feature in my app that uses a centered attributed UILabel with a proportionally spaced font to render time. At every time increment the width of the label changes, creating a bouncing effect that looks especially bad at fast speeds. Here is an example. How can I fix this? iOS 9 UPDATE It is now a one-liner: UIFont.monospacedDigitSystemFontOfSize(17, weight: UIFontWeightRegular) Also, last time I tried, the solution below did not work for iOS 9. Wasted quite a bit of time

Objective-C how to print out leading 0 for a float?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 02:10:15
问题 How do you print out leading zeros for a float using the NSString type? Input: 3.14 Desired output: 03.1 Using format @"%02.1f" Output: 3.1 回答1: You want @"04.1f" . The 4 is the total width. As you can see from the documentation, the format strings conform to the IEEE printf specification. The format string you've specified breaks down as follows: 0 -- Pad with zeros. 2 -- The entire resulting formatted value will have a minimum width of 2. .1 -- Precision of 1 digit following the decimal

iOS开发之视频播放功能、边播放边缓存

走远了吗. 提交于 2019-11-30 00:41:18
最近新做一个类似奖励视频的内置视频播放功能,并且实现边下载边播放,缓存后下次直接播放本地视频,自动适应横竖屏展示,给大家分享下核心代码 有不太清楚的地方可以加我微信一起探讨、主要六个文件如下 ECGRewardVideoView.h、 ECGRewardVideoView.m ECGPlayVideoResourceLoaderDelegate.h ECGPlayVideoResourceLoaderDelegate.m ECGPlayVideoRequestTask.h ECGPlayVideoRequestTask.m 代码如下,直接复制粘贴会有报错地方,但是我觉得你们肯定一看就懂的哈,如有不懂记得微信交流。 外部调用代码 参数说明:url-视频url、duration-播放时间、width-视频宽度、height-视频高度。 [[ECGRewardVideoView sharedInstance] showInsideRewardVideoWithUrl:url duration:duration width:width height:height]; ECGRewardVideoView.h代码,这是一个继承单例类 #import <UIKit/UIKit.h> #import "ECGBaseSingleInstance.h" @interface

How to determine if the first character of a NSString is a letter

荒凉一梦 提交于 2019-11-30 00:17:26
In my app i need to know if the first character of a string is a letter or not Im getting first character of the string like this NSString *codeString; NSString *firstLetter = [codeString substringFromIndex:1]; I can know it by comparing with a, b, c, .**. if([firstLetter isEqualToString "a"] || ([firstLetter isEqualToString "A"] || ([firstLetter isEqualToString "b"] ......) But is there any other method to know? I need to display different colors for letters and symbols. How can i achieve it in simple way? rmaddy First off, your line: NSString *firstLetter = [codeString substringFromIndex:1];

NSString with emoticons/emojis url encode

你说的曾经没有我的故事 提交于 2019-11-29 23:53:04
问题 I am trying to take the contents of a UITextField that may contain special characters and emojis and turn it into something I can pass in a GET request to a PHP service. If I do not encode the string at all, the emojis show up just fine (I can see them in the DB and they come back to me properly)... but if I add special chars (~!@#$%, etc.) the GET request chokes. So I run the string through the url encoder: [commentText stringByAddingPercentEscapesUsingEncoding:NSNonLossyASCIIStringEncoding]

Get current city and country from CLGeocoder?

你。 提交于 2019-11-29 23:21:40
I've been all over the internet trying to find out how to get the city and country from CLGeocoder . I can get the longitude and latitude easily but I need the city and country information, and I keep running into deprecated methods and such, any ideas? It basically needs to get the location, then have an NSString for the country, and an NSString for the city, so I can use them to look up more info or put them on labels, etc. You need to revise your terminology a bit - CLGeocoder (and most geocoders) won't give you a 'city' per-se - it uses terms such as 'Administrative Area',