nsstring

Padding NSString not working

时光怂恿深爱的人放手 提交于 2019-12-12 14:28:54
问题 I have read that to left-pad an NSString all you need to do is this: NSString *paddedStr = [NSString stringWithFormat:@"%-20.20@ %-20.20@", aString, anotherSting]; But, that does not work !! I don´t know why. I have tried a lot of combinations without success. Examples: NSString *paddedStr = [NSString stringWithFormat:@"%-20s@", " ", myString]; but that way is ugly and ... ugly. It just append 20 times the char (" ") before the string (myString) and that is not what we need right? The goal is

NSString stringWithFormat with tabs instead of spaces

本秂侑毒 提交于 2019-12-12 12:25:05
问题 You can see that I add 40 spaces after my string but it starts at index 0. Can I do the same thing with tab "\t" instead of spaces " "? NSString *firstString = [[NSString stringWithFormat:@"%@",stringToWrite] stringByPaddingToLength:40 withString:@" " startingAtIndex:0]; 回答1: Yes @"\t" will put tab instead of spaces. Also, your line could be a bit more simple : NSString *firstString = [stringToWrite stringByPaddingToLength:40 withString:@" " startingAtIndex:0]; 来源: https://stackoverflow.com

NSKeyedArchiver returning unexpected class?

依然范特西╮ 提交于 2019-12-12 12:10:05
问题 I have a custom class that extends NSString . I'm attempting to serialize it (for drag/drop) using an NSKeyedArchiver . The class overrides the ...Coder methods: - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { data = [[aDecoder decodeObjectForKey:@"data"] copy]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [super encodeWithCoder:aCoder]; [aCoder encodeObject:self.data forKey:@"data"]; } But when I try to actually run through the

Turning NSData back to plain text NSString

落爺英雄遲暮 提交于 2019-12-12 11:03:51
问题 I have a simple NSString and I'm converting it to NSData : NSData *data = [[NSData dataWithBytes:[[NSString stringWithFormat:@",%@",self.targetId] dataUsingEncoding:NSASCIIStringEncoding] length:[[NSString stringWithFormat:@",%@",self.targetId] lengthOfBytesUsingEncoding:NSASCIIStringEncoding]] retain]; Now I want to get the plain text from the NSData object. If I'm doing that : NSString* dataStr = [NSString stringWithFormat:@"%@",data]; - I'm getting just an hex expression (i.e <9c3f473e

NSArray and NSString

喜你入骨 提交于 2019-12-12 10:56:38
问题 The book I'm currently reading has me write the following code : -(IBAction)displaySomeText:(id)sender { NSString *cow = @"Milk"; NSString *chicken = @"Egg"; NSString *goat = @"Butter"; NSArray *food = [NSArray arrayWithObjects:cow, chicken, goat, nil]; NSString *string = @"The shopping list is: "; string = [string stringByAppendingString:[food componentsJoinedByString:@", "]]; [textView insertText:string]; } I understand somewhat how arrays work but I need help understanding the following

How to save to and read from a NSObject

佐手、 提交于 2019-12-12 10:23:05
问题 I have two NSObject's. @interface Car : NSObject{ @property (strong) NSSet *cars; } and @interface Model : NSObject{ @property (strong) UIImage *picture; @property (strong) NSString *name; } Basically the object Car has a NSSet cars and each object of the NSSet cars has the properties picture and name. How can I relate this two NSObject's and how can I save a string or image to the Car NSSet using the properties of the Model NSObject. Thanks. 回答1: For easy adding or removing, your " cars "

convert kABPersonPhoneProperty to arabic numbers

冷暖自知 提交于 2019-12-12 10:22:17
问题 I'm reading phone numbers from the phone address book , some numbers are saved there in non arabic numbers i.e ( ١٢٣٤٥٦٧٨٩ ) how can i convert these numbers to arabic numbers ( 123456789 ) that's how i extract the numbers CFTypeRef phoneProperty = ABRecordCopyValue(record, kABPersonPhoneProperty); NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty); now phone contains phone numbers as NSString objects 回答1: Tested: -(NSString*)arabicToWestern:

How do I get the substring between braces?

牧云@^-^@ 提交于 2019-12-12 09:39:58
问题 I have a string as this. NSString *myString = @"{53} balloons"; How do I get the substring 53 ? 回答1: NSString *myString = @"{53} balloons"; NSRange start = [myString rangeOfString:@"{"]; NSRange end = [myString rangeOfString:@"}"]; if (start.location != NSNotFound && end.location != NSNotFound && end.location > start.location) { NSString *betweenBraces = [myString substringWithRange:NSMakeRange(start.location+1, end.location-(start.location+1))]; } edit : Added range check, thx to Keab42 -

Highly Performant Objective C Alternatives to the Switch Statement for Objects

眉间皱痕 提交于 2019-12-12 08:56:40
问题 I have a function which I would like to take in an NSString and an int arguments and then use the switch statement in order to return a calculated value, as in multiply the int by some constant, depending on what NSString is supplied. Obviously, a switch statement doesn't work for objects in Objective-C. So what is the fastest alternative? Is it if-else statements? Or is there a more elegant method? EDIT The reason why I care about performance is that I am modifying UI elements the user is

Character occurrences in a String Objective C

前提是你 提交于 2019-12-12 08:23:52
问题 How can I count the occurrence of a character in a string? Example String: 123-456-7890 I want to find the occurrence count of "-" in given string 回答1: You can simply do it like this: NSString *string = @"123-456-7890"; int times = [[string componentsSeparatedByString:@"-"] count]-1; NSLog(@"Counted times: %i", times); Output: Counted times: 2 回答2: This will do the work, int numberOfOccurences = [[theString componentsSeparatedByString:@"-"] count]; 回答3: I did this for you. try this. unichar