nsstring

Objective-c convert Long and float to String

 ̄綄美尐妖づ 提交于 2019-11-30 07:58:08
I need to convert two numbers to string in Objective-C. One is a long number and the other is a float. I searched on the internet for a solution and everyone uses stringWithFormat: but I can't make it work. I try NSString *myString = [NSString stringWithFormat: @"%f", floatValue] for 12345678.1234 and get "12345678.00000" as output and NSString *myString = [NSString stringWithFormat: @"%d", longValue] Can somebody show me how to use stringWithFormat: correctly? This article discusses how to use various formatting strings to convert numbers/objects into NSString instances: String Programming

How to convert NSString to bytes

泪湿孤枕 提交于 2019-11-30 07:56:27
问题 NSString *test = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; How to convert this string to bytes? 回答1: NSData *bytes = [test dataUsingEncoding:NSUTF8StringEncoding]; 回答2: Do you want something like this: NSString *test=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; NSUInteger bytes = [test lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%i bytes", bytes); 回答3: Swift 1.x extension String { var bytes: [Byte] { return Array(utf8) } } Swift 2.1.1 extension String { var bytes : [UInt8] { return Array(utf8) } } Xcode

How to put a character limit on a UITextField

别说谁变了你拦得住时间么 提交于 2019-11-30 07:52:55
I would like to put a character limit on a UITextField but don't know how. I want it to have a maximum of 16 characters in it. How do I do this. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSUInteger newLength = [textField.text length] + [string length] - range.length; return (newLength > 12) ? NO : YES; } 来源: https://stackoverflow.com/questions/5760150/how-to-put-a-character-limit-on-a-uitextfield

Trying to Write NSString sha1 function, but it's returning null

主宰稳场 提交于 2019-11-30 07:38:02
问题 I have the following Objective-C function: +(NSString *)stringToSha1:(NSString *)str{ NSMutableData *dataToHash = [[NSMutableData alloc] init]; [dataToHash appendData:[str dataUsingEncoding:NSUTF8StringEncoding]]; unsigned char hashBytes[CC_SHA1_DIGEST_LENGTH]; CC_SHA1([dataToHash bytes], [dataToHash length], hashBytes); NSData *encodedData = [NSData dataWithBytes:hashBytes length:CC_SHA1_DIGEST_LENGTH]; [dataToHash release]; NSString *encodedStr = [NSString stringWithUTF8String:[encodedData

heightForRowAtIndexPath for longer NSStrings

时间秒杀一切 提交于 2019-11-30 07:24:49
I have a UITableView (Grouped!) and need to calculate the height of two styles of cells: UITableViewCellStyleDefault and UITableViewCellStyleValue2 . This is how I do it for UITableViewCellStyleDefault : CGSize textSize = {300.f, 200000.0f}; CGSize size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap]; size.height += 30.0f; result = MAX(size.height, 44.0f); And for UITableViewCellStyleValue2 : CGSize textSize = {207.f, 200000.0f}; CGSize size = [myTextString2 sizeWithFont:[UIFont systemFontOfSize:14.0f]

How to know the length of NSString that fits a UILabel with fixed size?

喜你入骨 提交于 2019-11-30 07:07:29
I know NSString has methods that determine the frame size for it, using NSString UIKit Additions, sizeWithFont...... How about the other way around? I mean if I have a fixed frame size, how do I know how many characters or words for a NSString that can fit into it? If I know this, I can cut off the NSString easily. thanks It might not be the most elegant solution, but you could do something like this: - (NSString *)string:(NSString *)sourceString reducedToWidth:(CGFloat)width withFont:(UIFont *)font { if ([sourceString sizeWithFont:font].width <= width) return sourceString; NSMutableString

stringByTrimmingCharactersInSet: is not removing characters in the middle of the string

♀尐吖头ヾ 提交于 2019-11-30 06:54:36
问题 I want to remove "#" from my string. I have tried NSString *abc = [@"A#BCD#D" stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"#"]]; But it still shows the string as "A#BCD#D" What could be wrong? 回答1: You could try NSString *modifiedString = [yourString stringByReplacingOccurrencesOfString:@"#" withString:@""]; 回答2: stringByTrimmingCharactersInSet removes characters from the beginning and end of your string, not from any place in it For your purpose use

Why does NSString respond to appendString?

本秂侑毒 提交于 2019-11-30 06:43:51
I was playing with the respondsToSelector method in Objective-C on MacOS-X 10.6.7 and Xcode 4.0.2, to identify if an object would respond to certain messages. According to the manuals, NSString should not respond to appendString: while NSMutableString should. Here's the piece of code which tests it: int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *myString = [[NSString alloc] init]; if ([myString respondsToSelector:@selector(appendString:)]) { NSLog(@"myString responds to appendString:"); } else { NSLog(@"myString doesn't respond

NSString with some html tags, how can I search for <img> tag and get the content of url

混江龙づ霸主 提交于 2019-11-30 05:43:32
I have a NSString with some html tags, how can I search for tag and get the content of url? I'm not sure if I must use Hpple or a simple Regex expression. In both cases can I have some example? One way to do this is using NSScanner . See this example: NSString *url = nil; NSString *htmlString = ... NSScanner *theScanner = [NSScanner scannerWithString:htmlString]; // find start of IMG tag [theScanner scanUpToString:@"<img" intoString:nil]; if (![theScanner isAtEnd]) { [theScanner scanUpToString:@"src" intoString:nil]; NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:

How to check the NULL value in NSString in iOS?

萝らか妹 提交于 2019-11-30 05:41:12
问题 I have an NSString and I want to check if it has a NULL value. If it does, then the if condition should execute. Else it should execute the else condition. Below is the code which I am using: if ([appDelegate.categoryName isEqual:[NSNull null]]) { select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID where ContentMaster.ContentTagText='%@'", appDelegate.tagInput]; } else { select = [[NSString alloc]