nsstring

iPhone: Return NSMutableArray in method while still releasing

余生长醉 提交于 2019-12-07 07:34:27
I am trying to understand a little more about memory management. Knowing that I need to release anything that I have init or alloc'ed I am confused about the following: - (NSMutableArray *)getData { NSMutableArray *data = [[NSMutableArray alloc] init]; NSString *first = @"First object"; [data addObject:first]; NSString *second = @"Second object"; [data addObject:second]; return data; } Since I used alloc and init, I know I need to release my data object. But if I add autorelease to the init part or to the return, it crashes when I run the method. What is the correct way to do something like

How to remove hexadecimal characters from NSString

六月ゝ 毕业季﹏ 提交于 2019-12-07 07:16:35
问题 I am facing one issue related some hexa value in string, i need to remove hexadecimal characters from NSString. The problem is when i print object it prints as "BLANK line". And in debug mode it shows like : So how can i remove it from the string? EDIT Triming whitespace : result of NSLog is : 2015-12-14 15:37:10.710 MyApp [2731:82236] tmp :'' Database: Earlier question: how to detect garbage string value in ios? 回答1: As your dataset clearly has garbage values, You can use this method to

NSString from NSArray

梦想与她 提交于 2019-12-07 06:02:59
问题 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 回答1: Another way to do this is to grab a mutable copy of the array and just remove non valid

How to convert a JSON String into an NSArray? [closed]

五迷三道 提交于 2019-12-07 05:55:31
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I am facing an issue while converting an NSString to NSArray. My string is : ["Default", "Discipleship", "Faith", "Family", "Hope", "Life Building", "Love", "Missions", "Relationships"] What i want to do is get the elements(Default,Discipleship etc.) out of this string and put them into an NSArray. I have tried

NSString stringwithformat: Padding 2 strings with unknown length

无人久伴 提交于 2019-12-07 05:43:58
问题 I'm having a problem doing something with NSString stringWithFormat... The problem is as follows: First of all, I have 2 strings. Which are money quantities. The thing, is that i want to create 1 string with these two. For ex: strF = [NSString stringWithFormat:@"First: %@ Second: %@", value1, value2]; I want to know if there is possible to make a correct padding between First and Second.. Assuming the first string is not the same length always.. I want to make this: First: 10,000.00 Second:

iOS FontAwesome in middle of another string

不问归期 提交于 2019-12-07 05:13:25
问题 I am trying to display some text and an icon inside a single string using FontAwesome. Here is what I have: NSString *icon = [NSString fontAwesomeIconStringForIconIdentifier:@"icon-map-marker"]; NSString *locationString = [NSString stringWithFormat:@"%@ %@", icon, otherNormalString]; Basically I want to have the map marker show up in front of the location being displayed. Using FontAwesome should make this really simple but I can't quite get it to work right. Here is also what shows up if I

How do I get class from NSString?

…衆ロ難τιáo~ 提交于 2019-12-07 04:44:22
问题 for example, I have NSString *myClass = @"RootViewController" How do I get class from NSString? 回答1: Try this id obj= [[NSClassFromString(@"RootViewController") alloc] init]; 回答2: You can use Class class = NSClassFromString(@"RootViewController"); NSClassFromString Obtains a class by name. Class NSClassFromString ( NSString *aClassName ); Parameters aClassName The name of a class. Return Value The class object named by aClassName, or nil if no class by that name is currently loaded. If

Convert NSCFString to NSString

孤街醉人 提交于 2019-12-07 04:39:37
问题 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

Correct use of format specifier to show up to three decimals if needed, otherwise zero decimals?

此生再无相见时 提交于 2019-12-07 04:18:40
问题 I've found %g to show only decimals if needed. If the number is whole, no trailing .000 is added, so thats good. But in the case of for example 1.12345 I want it to short the answer to 1.123. And in the case of 1.000 I want to only show 1, as %g already does. I've tried to specify %.3g in the string, but that doesn't work. If anyone has the answer, I'd be grateful! 回答1: I reviewed the abilities of a "format string" via the IEEE Specification and as I understand it your wished behavior is not

NSString stringWithContentsOfURL is deprecated. What should I do?

拈花ヽ惹草 提交于 2019-12-06 23:45:50
问题 I've written some code that uses stringWithContentsOfURL , but it turns out that it is now deprecated. Can you help me replace it? Here's my code: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]; NSArray *listItems = [locationString componentsSeparatedByString:@","]; Additionally, can someone have some sample code to show how it works? What encoding was the original code done in (in my 2 lines above)? I want to maintain compatibility so what