nsstring

Split NSString multiple times on the same separator

倾然丶 夕夏残阳落幕 提交于 2019-11-27 12:32:16
I am currently receiving a string like this: @"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54" And I am splitting it like this: testArray = [[NSArray alloc] init]; NSString *testString = [[NSString alloc] initWithFormat:@"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54,Steve|56,Matty|24,Bill|30,Rob|30,Jason|33,Mark|22,Stuart|54,Kevin|30"]; testArray = [testString componentsSeparatedByString:@","]; dict = [NSMutableDictionary dictionary]; for (NSString *s in testArray) { testArray2 = [s componentsSeparatedByString:@"|"]; [dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]]; }

Relative string from NSDate

╄→гoц情女王★ 提交于 2019-11-27 12:29:18
Does anyone know of a library or something that will convert an NSDate into strings like the examples below? 1 hour ago yesterday last Thursday 2 days ago last month 6 months ago last year David Schaefgen NSDateFormatter will do a great deal of what is mentioned above by using setDoesRelativeDateFormatting: on an instance. From there, you can control the formatting using the date style and the time style to fine tune it to your needs. If that doesn't get you what you need then check out SORelativeDateTransformer , it is an NSValueTransformer subclass that attempts to provide the same behavior

Objective-C - Finding a URL within a string

て烟熏妆下的殇ゞ 提交于 2019-11-27 12:25:53
问题 Given a large string, what is the best way to create an array of all valid urls which are contained within the string? 回答1: No need to use RegexKitLite for this, since iOS 4 Apple provide NSDataDetector (a subclass of NSRegularExpression ). You can use it simply like this ( source is your string) : NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil]; NSArray* matches = [detector matchesInString:source options:0 range:NSMakeRange(0, [source length

Is a literal NSString autoreleased or does it need to be released?

我的梦境 提交于 2019-11-27 12:19:40
问题 When creating a string using the following notation: NSString *foo = @"Bar"; Does one need to release foo ? Or is foo autoreleased in this case? 回答1: Compiler allocated strings (of the format @"STRING") are constant, and so -retain, -release, and -autorelease messages to them are ignored. You don't have to release or autorelease foo in this case (but it won't hurt). 回答2: As mentioned in the docs http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules

iOS开发---获取当前日期是星期几

断了今生、忘了曾经 提交于 2019-11-27 11:50:00
+ ( NSString *)currentDateWithFormatter:( NSString *)formatter { NSDate *date = [ NSDate date ]; NSDateFormatter *dateformatter = [[ NSDateFormatter alloc ] init ]; [dateformatter setDateFormat :formatter]; NSString *weekString = [dateformatter stringFromDate :date]; return weekString; } 当给formatter赋值为 @"EEEE"时, 打印出来的weekString即为想要获取的当前星期数(英文) 主要做法还是在时间格式那里.只不过是获取当前日期而已 获取日期时则把formatter赋值为@"yyyy-MM-dd HH:mm"就可以了 来源: CSDN 作者: Raymon-lau 链接: https://blog.csdn.net/sinat_28585351/article/details/50723975

iOS 获取当前日期是星期几

旧时模样 提交于 2019-11-27 11:48:41
在开发过程中,有时候需要查询当前日期是星期几。这个其实是分两个步骤:第一步是获取当前日期距离1970的时间,第二步是把这个时间转换为星期几。 -计算传入的时间是星期几 - ( NSString *)getWeekDayFordate:( NSTimeInterval )data { NSArray *weekday = [ NSArray arrayWithObjects: [NSNull null], @ "周日" , @ "周一" , @ "周二" , @ "周三" , @ "周四" , @ "周五" , @ "周六" , nil ]; NSDate *newDate = [ NSDate dateWithTimeIntervalSince1970:data]; NSCalendar *calendar = [[ NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:newDate]; NSString *weekStr = [weekday objectAtIndex:components .weekday ];

Get last path part from NSString

耗尽温柔 提交于 2019-11-27 11:44:09
问题 Hi all i want extract the last part from string which is a four digit number '03276' i:e http://www.abc.com/news/read/welcome-new-gig/03276 how can i do that. 回答1: You can also use NSString *sub = [@"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent]; 回答2: If you know how many characters you need, you can do something like this: NSString *string = @"http://www.abc.com/news/read/welcome-new-gig/03276"; NSString *subString = [string substringFromIndex:[string length] - 5];

NSString - Convert to pure alphabet only (i.e. remove accents+punctuation)

别等时光非礼了梦想. 提交于 2019-11-27 11:41:56
I'm trying to compare names without any punctuation, spaces, accents etc. At the moment I am doing the following: -(NSString*) prepareString:(NSString*)a { //remove any accents and punctuation; a=[[[NSString alloc] initWithData:[a dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding] autorelease]; a=[a stringByReplacingOccurrencesOfString:@" " withString:@""]; a=[a stringByReplacingOccurrencesOfString:@"'" withString:@""]; a=[a stringByReplacingOccurrencesOfString:@"`" withString:@""]; a=[a stringByReplacingOccurrencesOfString:@"-" withString:@""]; a

Converting HTML text into plain text using Objective-C

主宰稳场 提交于 2019-11-27 11:35:37
I have huge NSString with HTML text inside. The length of this string is more then 3.500.000 characters. How can i convert this HTML text to NSString with plain text inside. I was using scanner , but it works too slowly. Any idea ? It depends what iOS version you are targeting. Since iOS7 there is a built-in method that will not only strip the HTML tags, but also put the formatting to the string: Xcode 9/Swift 4 if let htmlStringData = htmlString.data(using: .utf8), let attributedString = try? NSAttributedString(data: htmlStringData, options: [.documentType : NSAttributedString.DocumentType

URLWithString: returns nil

半世苍凉 提交于 2019-11-27 11:31:06
it may be very easy, but I don't seems to find out why is URLWithString: returning nil here. //localisationName is a arbitrary string here NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false&key=", webName]; NSURL* url = [NSURL URLWithString:stringURL]; You need to escape the non-ASCII characters in your hardcoded URL as well: //localisationName is a arbitrary string here