Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-06-2010, 04:21 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default Converting String to Integer

Hi all,

I having been looking for a way of converting a string to an integer and in doing so I have read most if not all the posts concerning this process. After reading the OS reference documents I am no further to getting a solution.

What I ave is an SQL table and one of the fields contains "time" data which could be in the format of "+10:00" or "-4:30". In ideal circumstances the data I am being feed would be of the "int" type but unfortunately it is of the type "varchar". I need to split the string into parts so I can add the hours and minuets to the current time.

I have written the code for the split but I think I need to change the data from a string to an integer, is this possible, if so can anyone point me in the right direction.

My code so far is:
Code:
NSRange plusminus = NSMakeRange(3, 2);
	NSString *pm = [OffSet1 substringWithRange:plusminus];
	
	NSRange offSetHours = NSMakeRange(5, 3);
	NSString *offSetH = [OffSet1 substringWithRange:offSetHours];
	
	NSRange offSetMins = NSMakeRange(9, 3);
	NSString *offSetM = [OffSet1 substringWithRange:offSetMins];
	
	NSLog(@"The contents of Plus or Minus = %@",[pm  description]);
	NSLog(@"The contents of Offset Hours = %@",[offSetH  description]);
	NSLog(@"The contents of Offset Mins= %@",[offSetM  description]);
	
	
	NSDate *date = [NSDate date];
	NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
	[formatter setDateFormat:@"HH:MM:SS"];
	NSString* str =[formatter stringFromDate:date];
	
	
	NSRange NowOffSetHours = NSMakeRange(5, 3);
	NSString *NowOffSetH = [OffSet1 substringWithRange:NowOffSetHours];
	
	NSRange NowOffSetMins = NSMakeRange(9, 3);
	NSString *NowOffSetM = [OffSet1 substringWithRange:NowOffSetMins];
	

	NSLog(@"The contents of now Hours = %@",[NowOffSetH  description]);
	NSLog(@"The contents of Mins Mins= %@",[NowOffSetM  description]);
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-06-2010, 04:40 PM   #2 (permalink)
CAM
Registered Member
 
Join Date: Feb 2009
Posts: 157
CAM is on a distinguished road
Default

Is this what you need
Code:
NSString *string;
string = @"22";
int *intFromString;
intFromString = [string intValue];
intFromString should now be 22
CAM is offline   Reply With Quote
Old 08-06-2010, 05:04 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi Cam,

Many thanks for your reply.

I tried out your code as is but when run it produces the follow warning:

"Assignment makes pointer from integer without a cast"
Code:
NSString *string;
string = @"22";
int *intFromString;
intFromString = [string intValue]; // Warning on this line.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-06-2010, 05:12 PM   #4 (permalink)
CAM
Registered Member
 
Join Date: Feb 2009
Posts: 157
CAM is on a distinguished road
Default

sorry there was a mistake in my code

this is the updated code
Code:
NSString *string;
string = @"22";
int intFromString;
intFromString = [string intValue];
The problem with my code was this line
Code:
int *intFromString;
i screwed up by putting an * in front of the int name. That turned it into a pointer and you don't want an int to be a pointer.
CAM is offline   Reply With Quote
Old 08-09-2010, 12:32 AM   #5 (permalink)
Registered Member
 
fousulameen's Avatar
 
Join Date: Jul 2010
Location: chennai
Posts: 41
fousulameen is on a distinguished road
Send a message via Skype™ to fousulameen
Default convert string to int

Quote:
Originally Posted by dcjones View Post
Hi all,

I having been looking for a way of converting a string to an integer and in doing so I have read most if not all the posts concerning this process. After reading the OS reference documents I am no further to getting a solution.

What I ave is an SQL table and one of the fields contains "time" data which could be in the format of "+10:00" or "-4:30". In ideal circumstances the data I am being feed would be of the "int" type but unfortunately it is of the type "varchar". I need to split the string into parts so I can add the hours and minuets to the current time.

I have written the code for the split but I think I need to change the data from a string to an integer, is this possible, if so can anyone point me in the right direction.

My code so far is:
Code:
NSRange plusminus = NSMakeRange(3, 2);
	NSString *pm = [OffSet1 substringWithRange:plusminus];
	
	NSRange offSetHours = NSMakeRange(5, 3);
	NSString *offSetH = [OffSet1 substringWithRange:offSetHours];
	
	NSRange offSetMins = NSMakeRange(9, 3);
	NSString *offSetM = [OffSet1 substringWithRange:offSetMins];
	
	NSLog(@"The contents of Plus or Minus = %@",[pm  description]);
	NSLog(@"The contents of Offset Hours = %@",[offSetH  description]);
	NSLog(@"The contents of Offset Mins= %@",[offSetM  description]);
	
	
	NSDate *date = [NSDate date];
	NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
	[formatter setDateFormat:@"HH:MM:SS"];
	NSString* str =[formatter stringFromDate:date];
	
	
	NSRange NowOffSetHours = NSMakeRange(5, 3);
	NSString *NowOffSetH = [OffSet1 substringWithRange:NowOffSetHours];
	
	NSRange NowOffSetMins = NSMakeRange(9, 3);
	NSString *NowOffSetM = [OffSet1 substringWithRange:NowOffSetMins];
	

	NSLog(@"The contents of now Hours = %@",[NowOffSetH  description]);
	NSLog(@"The contents of Mins Mins= %@",[NowOffSetM  description]);


my code:

NSString *string=@"5";
NSInteger value;
value=[string intValue];
NSLog(@"%d",value);


and there is no pointer for integer datatype.....
fousulameen is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 308
11 members and 297 guests
alexP, arash5500, gordo26, HemiMG, linkmx, mediaspree, nobstudio, Objective Zero, Sloshmonster, stanny, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:29 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0