nsstring

Converting NSDecimalNumber to NSString

大兔子大兔子 提交于 2019-12-03 07:11:50
问题 I'm retrieving a key from an object that looks like this: po obj { TypeID = 3; TypeName = Asset; } The key value is being retrieved like this: NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"]; Rather than typeId being an NSString, it is an NSDecimalNumber. Why is that? How do I convert it to an NSString? 回答1: You need to use the stringWithFormat function: NSString *typeId = [NSString stringWithFormat:@"%@", [obj objectForKey:@"TypeID"]]; or stringValue : NSString *typeId = [[obj

Objective C: convert a NSMutableString in NSString

混江龙づ霸主 提交于 2019-12-03 06:27:57
问题 I have an NSMutableString, how can I convert it to an NSString? 回答1: Either via: NSString *immutableString = [NSString stringWithString:yourMutableString]; or via: NSString *immutableString = [[yourMutableString copy] autorelease]; //Note that calling [foo copy] on a mutable object of which there exists an immutable variant //such as NSMutableString, NSMutableArray, NSMutableDictionary from the Foundation framework //is expected to return an immutable copy. For a mutable copy call [foo

Extract 2 strings from an NSString separated by a special character

╄→尐↘猪︶ㄣ 提交于 2019-12-03 06:09:46
i have an NSString like "Hello - this is me". I want to search for the "-" and put the text before and after the "-" in two separate strings. Anyone knows how to do that the best way? greets Max NSArray *subStrings = [myString componentsSeparatedByString:@"-"]; //or rather @" - " NSString *firstString = [subStrings objectAtIndex:0]; NSString *lastString = [subStrings objectAtIndex:1]; //Add some array range checking to it and you're done. NSString *myString = @"123-456-789-1234-2345-3456-4567"; NSArray *subStrings = [myString componentsSeparatedByString:@"-"]; for (int i = 0; i < [subStrings

stringWithFormat vs. initWithFormat on NSString

╄→гoц情女王★ 提交于 2019-12-03 05:53:14
问题 I am wondering what differences such as disadvantages and/or advantages there are to declaring an NSString this way: NSString *noInit = [NSString stringWithFormat:@"lolcatz %d", i]; as opposed to: NSString *withInit = [[NSString alloc] initWithFormat:@"Hai %d", i]; What was the motivation of putting stringWithFormat instead of just having the initWithFormat way of initializing the string? 回答1: stringWithFormat: returns an autoreleased string; initWithFormat: returns a string that must be

Emojis messing with obj-c's sizeWithFont math

梦想的初衷 提交于 2019-12-03 05:51:10
问题 In a UITableView that needs to display a long list of chatlike conversations, often containing emojis, a size calculation error occurs. My problem is, that if a string is just the right length, and I use sizeWithFont , I on my first measurement using sizewithfont get an incorrect length of the string, causing a "linebreak". I assume that it is because the string ":-)" is broader than the actual smiley icon. The evidence can be seen here : Now, over at some other stacks, some claim that

How to find the text in an NSTextView?

…衆ロ難τιáo~ 提交于 2019-12-03 05:50:16
I need to find the text in an NSTextView , and save it to a file. I can do the saving fine. I have used -stringValue , and -textStorage so far, but neither have worked. When I put -stringValue in, it just gave me (null), and when I put -textStorage in, it gave me a very large chunk of nothing (like a long paragraph of invisible text). How can I put the text from an NSTextView into an NSString ? Try NSString *text = [[myTextView textStorage] string]; The NSTextStorage inherits from NSMutableAttributedString which inherits from NSAttributedString . The latter implements the string method. This

casting NSInteger into NSString

非 Y 不嫁゛ 提交于 2019-12-03 05:41:23
I'm trying to make one string out of several different parts. This below is what i have right now. I don't get any errors in my code but as soon as i run this and do the event that triggers it i get EXC_BAD_ACCESS . Is there another way of casting this NSInteger into a NSString ? NSString *part1, *part2, *tempString; NSInteger num1; NSInteger num2; part1=@"some"; part2=@"text"; tempString = [NSString stringWithFormat:@"%@%@%@%@", part1, (NSString *)num1, part2, (NSString *)num2]; A string and an integer are fundamentally different data types, and casting in Objective-C won't do a conversion

How to make an NSString path (file name) safe

孤人 提交于 2019-12-03 05:32:05
问题 I'm using very tricky fighting methods :) to make a string like Fi?le*/ Name safe for using as a file name like File_Name . I'm sure there is a cocoa way to convert it. And I'm sure the best place to ask is here :) Thank you! 回答1: Unless you're explicitly running the shell or implicitly running the shell by using a function such as popen or system , there's no reason to escape anything but the pathname separator. You may also want to enforce that the filename does not begin with a full stop

switch case on NSString in objective c [duplicate]

扶醉桌前 提交于 2019-12-03 05:21:31
问题 This question already has answers here : Using an NSString in a switch statement (9 answers) Closed 6 years ago . i want to use case statement with NSString please change my code to correct code NSString *day = @"Wed"; switch (day) { case @"Sat": NSlog(@"Somthing..."); break; case @"Sun": NSlog(@"Somthing else..."); break; . . . . default: break; } 回答1: If you want some slightly smarter dispatch than a long list of conditionals you can use a dictionary of blocks: NSString *key = @"foo"; void

How to convert byte array to NSString

余生颓废 提交于 2019-12-03 05:20:02
问题 I am reading data from a TCP/IP stream and am successfully receiving a byte array from the pre-existing server. I am now trying to find a way to convert that array to an NSString . I have found several examples, but am having a hard time getting my desired results. NSData *data=[[NSMutableData alloc] init]; uint8_t buffer[1024]; unsigned int len=0; len=[(NSInputStream *)stream read:buffer maxLength:1024]; if(len>0){ [data appendBytes:&buffer length:len]; //BYTE ARRAY OBTAINED OK!! ///////////