天道酬勤,学无止境

nsstring

NSString from NSArray

I am trying to create a String from Array.But, there is condition to how it should be generated, as explained below. NSArray *array=[NSArray arrayWithObjects:@"Hello",@"World",nil]; [array componentsJoinedByString:@","]; This will output: Hello,World. But, if first Item is Empty,then is there way to receive the only second one. Hello , @"" => Hello @"" , World => World Hello , World => Hello,World

2021-06-15 15:13:46    分类:问答    ios   objective-c   xcode   nsstring   nsarray

'(NSObject, AnyObject)' is not convertible to 'String'

How do I convert an object of type (NSObject, AnyObject) to the type String? At the end of the first line of the method below, as String causes the compiler error: '(NSObject, AnyObject)' is not convertible to 'String' Casting street to NSString instead of String compiles, but I'm casting street to String because I want to compare it to placemark.name, which has the type String!, not NSString. I know name and street are optionals, but I'm assuming they're not nil for now because all the places returned from MKLocalSearch seem to have non-nil names and streets. func formatPlacemark(placemark

2021-06-15 02:20:14    分类:问答    string   dictionary   swift   nsstring   optional

Adding double quotes to NSString

I have reading everything and \" does NOT work. I am trying to create a string that looks like this: "SomeString" = "AnotherString" Here is the code that I have to insert the double quotes: NSString *newOutput = [NSString stringWithFormat:@"%@ \" = \" %@", output, [englishDic objectForKey:str]]; All that it outputs is: "RateThisAppDontAsk \" = \" Don't ask again" I thought maybe the "=" was causing problems but removing still gives me an output of this: "RateThisAppDontAsk \" \" Don't ask again" Any help would be very much appreciated!

2021-06-15 00:20:37    分类:问答    objective-c   nsstring   formatting

Convert NSCFString to NSString

I am getting a dictionary from server myDictionary = { "rank":"1", "color":"red", "position":"middle" } Now I want to check the value of key "position" in if condition I am doing it like this if ([[myDictionary valueForKey:@"position"] isEqualToString:@"middle"]) { //do Some stuff } else{ //do some other stuff } but data type of [myDictionary valueForKey:@"position"] is _NSCFString, so it does not compare value properly and never goes in if loop even the value is correct. how do I convert it into NSString so that I could compare it in if condition ? I have seen these questions.. NSString

2021-06-14 16:57:50    分类:问答    ios   objective-c   nsstring   nscfstring

Remove backslash ('\') in objective-c

I am generating json text and while generating array I get a lot of backslashes I don't need: [\n {\n \"Speed\" : 2,\n \"Direction\" : 3,\n \"OdometerDelta\" : 4,\n \"Longitude\" : 0,\n \"Latitude\" : 1,\n \"TimeStamp\" : \"1996-06-17\"\n },\n {\n \"Speed\" : 2,\n \"Direction\" : 3,\n \"OdometerDelta\" : 4,\n \"Longitude\" : 0,\n \"Latitude\" : 1,\n \"TimeStamp\" : \"1996-06-17\"\n },\n {\n \"Speed\" : 2,\n \"Direction\" : 3,\n \"OdometerDelta\" : 4,\n \"Longitude\" : 0,\n \"Latitude\" : 1,\n \"TimeStamp\" : \"1996-06-17\"\n }\n ] This is how I get my generated json text NSString: NSData

2021-06-14 13:55:42    分类:问答    objective-c   nsstring

Appending unichar to NSMutableString

How do you append a unichar character to NSMutableString? unichar c = 'T'; NSMutableString *s = [NSMutableString stringWithString:@"MyString"]; // want s to become "MyStringT" https://discussions.apple.com/thread/1679290 suggests: [s appendString:[NSString stringWithCharacters:&c length:1]]; Looks too long / complicated...

2021-06-14 09:08:43    分类:问答    objective-c   nsstring   char   nsmutablestring   unichar

In Objective-C, how to print out N spaces? (using stringWithCharacters)

The following is tried to print out N number of spaces (or 12 in the example): NSLog(@"hello%@world", [NSString stringWithCharacters:" " length:12]); const unichar arrayChars[] = {' '}; NSLog(@"hello%@world", [NSString stringWithCharacters:arrayChars length:12]); const unichar oneChar = ' '; NSLog(@"hello%@world", [NSString stringWithCharacters:&oneChar length:12]); But they all print out weird things such as hello ÔÅÓñüÔÅ®Óñü®ÓüÅ®ÓñüÔ®ÓüÔÅ®world... I thought a "char array" is the same as a "string" and the same as a "pointer to a character"? The API spec says it is to be a "C array of Unicode

2021-06-13 21:15:49    分类:问答    objective-c   ios   nsstring   core-foundation

Split string by delimiter

I really can't find this answer... I have multiline NSString called myString in XCode, and it is a HTML code. I need to navigate the string by lines, for example: myString = @"<html>" "<body>" "<head><title>My Page</title>"; How can I access line per line? like: LineOne = myString.Lines[0]; LineTwo = myString.Lines[1]; How can I do something like that in XCode??? I need something like the Memo component in Delphi...

2021-06-13 18:09:25    分类:问答    objective-c   ios   xcode   nsstring   multiline

Find the index of a character in a string

I have a string NSString *Original=@"88) 12-sep-2012"; or Original=@"8) blablabla"; I want to print only the characters before the ")" so how to find the index of the character ")". or how could i do it? Thanks in advance.

2021-06-13 17:39:57    分类:问答    iphone   objective-c   ios   nsstring

stringByAppendingFormat not working

I have an NSString and fail to apply the following statement: NSString *myString = @"some text"; [myString stringByAppendingFormat:@"some text = %d", 3]; no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString. any clues most welcome.

2021-06-13 13:53:42    分类:问答    objective-c   nsstring   string-concatenation