nsstring

NSString concatenate on creation

本小妞迷上赌 提交于 2019-12-24 11:26:22
问题 I'm trying to concatenate 2 strings assigning the result to a new string. Normally I would do this way: NSString * s = [NSString stringWithFormat: @"%@%@", str1, str2]; Now I wish s to be static static NSString * s = [NSString stringWithFormat: @"%@%@", str1, str2]; but compiler kick me with "Initializer element is not a compile-time..." Is there any way to do this? I Googled a bit with no results and also I have not found answers on StackOverflow asking the question. And what about using a

Position of a character NSString

孤街醉人 提交于 2019-12-24 10:49:07
问题 I have string which contains either A ,B, C, or D (example A123 or B235 or 2B35 but not AB123) I want to find the index of A,B,C or D In C # we write as String s = "123B"; index = s.IndexOfAny(new char[] = {A,B,C,D}); How to write in Objective-C?? 回答1: You can use -rangeOfCharacterFromSet:: NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCD"]; NSRange range = [string rangeOfCharacterFromSet:charSet]; if (range.location == NSNotFound) { // ... oops } else { //

NSString to SecKeyRef

不打扰是莪最后的温柔 提交于 2019-12-24 10:35:24
问题 I'm using this code: https://stackoverflow.com/a/19221754/849616, however not everything is clear for me. I want to encrypt NSString *msg = "0000" using public key NSString *pubKey = "1111" . Because of this, I'm updating constants: static const UInt8 publicKeyIdentifier[] = 1111; // i want to encrypt only, so private key doesn't matter and I'm not posting it here In function testAsymmetricEncryptionAndDecryption I've updated: const char inputString[] = 0000 However the result is wrong. Is

Issue with Core Data attribute returning 0 [duplicate]

痞子三分冷 提交于 2019-12-24 10:18:53
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Core Data not saving NSString. Hey all! Currently experiencing a weird issue with Core Data in my latest application. Basically what I'm accomplishing is that of parsing JSON and adding each object into Core Data under the respective attribute. This works fine as far as I can see from the NSLogs I have set in place. However, when it comes to displaying one of the values in a UITableView, it always returns 0 for

Allocation and release problem

▼魔方 西西 提交于 2019-12-24 09:28:28
问题 I'm trying to explain memory management on iOS to a friend and I'm showing him a wrong code. But when I'm launching the app, it's working and I don't know why. Here's the snippet : NSString *myString = [[NSString alloc] initWithString:@"myString"]; [myString release]; NSLog(@"%@",myString); I don't understand why my NSLog is working ... Do you have some explanations ? Thanks ! 回答1: This is an undefined behavior. You are not allowed to access this string, yet it might be available. 回答2: There

iOS DDMathParser Get Number 'Blocks'

可紊 提交于 2019-12-24 08:55:24
问题 I am building a calculator app for iOS using DDMathParser. I would like to know how to separate an expression ( NSString ) like 5+900/32 into its individual number values: 5 , 900 , and 32 . I would prefer that I get these in an NSArray or other relatively useful list object. I have heard that there is an easy way to do this with DDMathParser - It's why I switched from GCMathParser . 回答1: DDMathParser author here. Yes, this is very easy, and there are a couple of ways to go about it. If you

Convert a NSString into the name of a Constant

半世苍凉 提交于 2019-12-24 08:23:28
问题 I have a bunch of constants declared like this: #define kConstant0 @"Cow" #define kConstant1 @"Horse" #define kConstant2 @"Zebra" Elsewhere in code I'm trying to extract the constant value by adding an integer to the string name of the constant: int myInt = 1; // (Actual intValue Pulled From Elsewhere) myLabel.text = [@"kConstant" stringByAppendingString:[NSString stringWithFormat:@"%i",myInt]]; But of course this returns: myLabel.text = @"kConstant1"; When I want it to return: myLabel.text =

Convert integer to string and use each character

隐身守侯 提交于 2019-12-24 08:19:14
问题 How is an integer converted to a string in objective c and how is each character of the string reached? The equivalent to the java charAt() method. 回答1: NSString *myString = [NSString stringWithFormat:@"%i", 36]; [myString characterAtIndex:0]; //"3" [myString characterAtIndex:1]; //"6" Update : Note that characterAtIndex returns a unichar not a c char. Thanks @Chuck. 来源: https://stackoverflow.com/questions/4541616/convert-integer-to-string-and-use-each-character

Different colors in UITextView

夙愿已清 提交于 2019-12-24 08:13:45
问题 I try to add string with different colors in my UITextView. I wrote this code NSMutableAttributedString* attString = [[NSMutableAttributedString alloc]initWithString:view.text]; //view is my UITextView [attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:[UIColor greenColor] range:(NSRange){attString.length-8, 8}]; view.attributedText = attString; Maybe it is incorrect attribute, can you tell me what attribute change color for text? 回答1: Try using

NSPredicate Character and/or Letter Substitutions

血红的双手。 提交于 2019-12-24 07:09:42
问题 I've seen some, somewhat, similar questions about filtering out special characters in a predicate - but nothing suitable for my needs. I'm not really sure if what I want to do is even possible but I'll put it to the community instead: I'm working with strings of pronunciations for a dictionary that contain a lot of IPA characters for instance: ȵ ɕ ŋ Ẓ What I would like to do is set up some substitutions for these characters, something like: n=ȵ x=ɕ ng=ŋ r=Ẓ in a search predicate so that a