nsstring

Exponents in NSString

情到浓时终转凉″ 提交于 2019-12-11 09:06:49
问题 Is it possible to have exponents in an NSString object that will be displayed in a UITextField and a UILabel? If so, how do we do it? If not, any alternatives you might suggest? 回答1: As answered by jrturton in this question: You need to include the unicode symbol for a superscripted character: NSInteger number = 10; NSString *cubedSymbol = @"\u00B3"; NSString *tenCubed = [NSString stringWithFormat:@"%d%@",number,cubedSymbol]; Plenty more fun is to be had here 来源: https://stackoverflow.com

NSMutableString modify while iterating text checking results

本秂侑毒 提交于 2019-12-11 09:01:06
问题 I'm trying to modify an NSString while iterating NSTextCheckingResults from an NSRegularExpression . I know that it won't work the way I implemented it as every replacement changes the length of the string an so the validity of the NSRages in my loop. How can I replace multiple matches in a for loop? Here is my code: NSMutableString *string = [@"[H]…[mm]…[s]" mutableCopy]; NSReguralExpression *exp = [NSRegularExpression regularExpressionWithPattern:@"(\\[[Hms]{1,2}\\])" options:0 error:nil];

Some chars changed in NSString

我的梦境 提交于 2019-12-11 08:52:05
问题 I'm retrieving a string from url content, this way: NSString *urlStr = [NSString stringWithFormat:@"http://www.example.com/string_to_retrieve"]; NSURL *url = [NSURL URLWithString:urlStr]; NSString *resp = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil]; and writing it to file, this way: NSString *outFile = @"/path/to/myfile"; [resp writeToFile:outFile atomically:YES encoding:NSUTF8StringEncoding error:nil]; my string contains several special chars like "ö"

Cocoa String Question

家住魔仙堡 提交于 2019-12-11 08:48:27
问题 I have a NSString and I want to write its value to a NSMutableString. Is this valid: NSString *temp = [NSString stringWithString:@"test"]; NSMutableString *mutable = temp; I ask because although this seems doable, I would think that this would assign both temp and mutable to the same address space. I have this question a lot when passing values into a method or returning values from a method. Sometimes I see other people do this or create the mutable string with stringWithString or

How to get a string of a particular height in objective c

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 08:38:01
问题 Hi in my application I have a lengthy string. From that string I have to substring a part of a string where the string height to be 500. So please let me know how to get the a part of a string. For your reference I am sharing the code what I used for getting string height CGSize finalStringSize = [finalString sizeWithFont:font constrainedToSize:CGSizeMake(195,length) lineBreakMode:NSLineBreakByWordWrapping]; Here finalstringSize.height is 500. Now any one help me to get a substring from final

sorting NSArray of NSStrings by match occurrence

和自甴很熟 提交于 2019-12-11 08:33:34
问题 How can I sort NSArray by search pattern matching? So if for example I have a search pattern equal 'xd' and an array of values: axd bxd xdd gtxd xdc how can I get the output like below: xdc xdd axd bxd gtxd Thank you in advance. 回答1: Use NSArray 's sortedArrayUsingFunction: with a function that orders first by the position of the search term, then by the strings' natural ordering. NSInteger sorter(id arg1, id arg2, void *context) { NSString *searchTerm = (NSString *)context; NSRange range1 =

How to create randomly selected NSString “package” for iPhone app

风格不统一 提交于 2019-12-11 08:17:45
问题 I'm just delving into Objective C and Cocoa Touch and I'm trying to build an app for personal use. My goal is to create an app that displays a randomized NSString in a center window on the iPhone screen while also displaying a scrollable list of associated NSStrings in another window on the side of the screen. For example: if the center NSString is the name of an animal, such as "Lion", the NSStrings on the list next to it would also be animals (e.g., "Tiger", Snow Leopard", etc.) I would

How to read a text file ,turn it into a string, then using arguments on it

妖精的绣舞 提交于 2019-12-11 08:01:04
问题 Say If I had a string = "There were %@ apples,I ate %@. Now I have %@ apples" in text file "Apples.txt". This is how I will extract the text from it. NSString *path = [[NSBundle mainBundle] pathForResource:@"Apples" ofType:@"txt"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; Now how do I pass the arguments to it so it looks like this: "There were %@ apples,I ate %@. Now I have %@ apples", x, y, x-y I am sure there is a way around this

IOS Weird compiler error: "expected ']' before ;' using set-ter for NSString constant in singleton

走远了吗. 提交于 2019-12-11 07:48:58
问题 I have the weirdest error! In Xcode I have a singleton with the following defined (file is: MyGizmoClass.h ): NSString *plistPath; NSString *dataDomain; NSString *pathToChatScript; NSString *pathToUpdates; and @property (nonatomic,retain) NSString *plistPath; @property (nonatomic,retain) NSString *dataDomain; @property (nonatomic,retain) NSString *pathToChatScript; @property (nonatomic,retain) NSString *pathToUpdates; I have a Constants.h file (which I #import early in my .pch file) that

Get a char from NSString and convert to int

╄→尐↘猪︶ㄣ 提交于 2019-12-11 07:45:23
问题 In C# I can convert any char from my string to integer in the following manner intS="123123"; int i = 3; Convert.ToInt32( intS[i].ToString()); What is the shortest equivalent of this code in Objective-C ? The shortest one line code I've seen is [NSNumber numberWithChar:[intS characterAtIndex:(i)]] 回答1: Many interesting proposals, here. This is what I believe yields the implementation closest to your original snippet: NSString *string = @"123123"; NSUInteger i = 3; NSString