//string convert into integer
numberIntValue=[numberId intValue];
numberIntValue=16153;
How can i split this integer number into 16, 15 and 3?
NSInteger one=numberIntValue/1000;
NSInteger two=(numberIntValue%1000)/10;
NSInteger three=numberIntValue%10;
Related
I have NSArray with strings - 00:02:34, 02:05:17 so i need to calculate all strings in my array and get result: 2 hours:7 minutes and 51 seconds.
I tried this:
// Get strings from array for the first and separate for three objects by:
for (NSDictionary *dic in dicts) {
NSString *string = dic[#"duration"]; (my type of string 00:00:00)
NSArray *components = [string componentsSeparatedByString:#":"];
NSInteger minutes = [components[1] integerValue];
NSInteger seconds = [components[2] integerValue];
NSInteger hour = [components[0] integerValue];
}
But how can i summ this date to get results? Thanks for help.
There are a few different ways you could approach this.
Personally, I'd iterate through dicts and convert each duration string to seconds and keep count of the total seconds in an integer outside of the loop.
Then, you can easily convert the cumulative seconds total back into hours, minutes and seconds and compose a string from them, after the loop is complete:
int totalSeconds = 0;
for (NSDictionary * dic in dicts) {
NSString *string = dic[#"duration"];
NSArray *components = [string componentsSeparatedByString:#":"];
totalSeconds += (int) [components[0] integerValue] * 60 * 60;
totalSeconds += (int) [components[1] integerValue] * 60;
totalSeconds += (int) [components[2] integerValue];
}
int hour = totalSeconds / 3600;
int mins = (totalSeconds % 3600) / 60;
int secs = totalSeconds % 60;
NSString * totalString = [NSString stringWithFormat:#"%d:%d:%d", hour, mins, secs];
Note: you'll have to write a bit of code to compose the string and include zeros as appropriate, where any of the values are less than 10.
I'm trying to get the amount of lines in a TextView. I've searched here for a solution, basically every thread has the same answer : contentheight/fontlineheight.
I have a TextView with 8 lines, i run this code and i get contsize : 1.944413
NSLog(#"contsize : %f", descLabel.contentSize.height/descLabel.font.lineHeight);
What am i doing wrong?
For iOS7, a version that take care that you can have differents font (and font size) in your UITextView :
- (NSUInteger)numberOfLinesInTextView:(UITextView *)textView
{
NSLayoutManager *layoutManager = [textView layoutManager];
NSUInteger index, numberOfLines;
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:[textView textContainer]];
NSRange lineRange;
for (numberOfLines = 0, index = glyphRange.location; index < glyphRange.length; numberOfLines++){
(void) [layoutManager lineFragmentRectForGlyphAtIndex:index
effectiveRange:&lineRange];
index = NSMaxRange(lineRange);
}
return numberOfLines;
}
In Xcode i am trying to make a graph into which some points (X,Y) are given by the user.The user can choose how many points he or she wants to plot on the graph. I have stored the X axis points in an NSMutableArray and the Y axis points in an NSMutableArray but now i need to sort these into ascending order so i can fix a bug with the graph when having two points with the same x or y axis plot. The NSMutableArrays contains primitive int's. An example of what i need to do is if i am given 4 coordinates for one of the axis such as 4,8,5,3 i need the array to be sorted into 3,4,5,8
The code below doesn't include the graph generation because it is quite long
Thanks for the help! -
int userInputCount;
printf("How many coordinates would you like to plot? ");
scanf("%i", &userInputCount);
printf("\n");
NSMutableArray * xCoordArray = [[NSMutableArray alloc] init];
NSMutableArray * yCoordArray = [[NSMutableArray alloc] init];
for (int i = 1; i <= userInputCount; i++){
int xCoord;
int yCoord;
printf("(%i) Enter coordinates [X,Y]: ", i);
scanf("%i,%i", &xCoord, &yCoord);
NSNumber *xCoordinate = [[NSNumber alloc] initWithInt:xCoord];
NSNumber *yCoordinate = [[NSNumber alloc] initWithInt:yCoord];
[xCoordArray addObject:xCoordinate];
[yCoordArray addObject:yCoordinate];
}
After working on it for a while I figured it out if anybody is trying the same thing
for (int j = 0; j < 2; j++){
for (int i = 0; i < [yCoordArray count] - 1; i++){
int yExchangeIntOne = [[yCoordArray objectAtIndex:i] intValue];
int yExchangeIntTwo = [[yCoordArray objectAtIndex:i +1] intValue];
if (yExchangeIntOne > yExchangeIntTwo){
[yCoordArray exchangeObjectAtIndex:i+1 withObjectAtIndex:i];
i = 0;
}
int xExchangeIntOne = [[xCoordArray objectAtIndex:i] intValue];
int xExchangeIntTwo = [[xCoordArray objectAtIndex:i +1] intValue];
if (xExchangeIntOne > xExchangeIntTwo){
[xCoordArray exchangeObjectAtIndex:i+1 withObjectAtIndex:i];
i = 0;
}
}
}
This goes right after the
[xCoordArray addObject:xCoordinate]; and the
[yCoordArray addObject:yCoordinate]; in the loop
I´m trying to set a random number in a string, but idk how i can let the program know that i want the random number and not the letter n.
Im using visual studio 2008 , Windows Forms C++
System::Drawing::Font ^Fuente = gcnew System::Drawing::Font("Arial Black",50);
System::Random ^r = gcnew System::Random(System::DateTime::Now.Ticks);
char n=r->Next(1,100);
buffer->Graphics->DrawString("n",Fuente,System::Drawing::Brushes::WhiteSmoke,50,50);,50);
System::Drawing::Font ^Fuente = gcnew System::Drawing::Font("Arial Black",50);
System::Random ^r = gcnew System::Random(System::DateTime::Now.Ticks);
int n=r->Next(1,100);
buffer->Graphics->DrawString(n.ToString(), Fuente, System::Drawing::Brushes::WhiteSmoke,50,50);,50);
Might be what you are after
You're using quotes when drawing string - that's a string literal.
You should convert your number to a string using stdlib.h itoa function.
char number[3];
int n = r->Next(1,100);
itoa(n, number, 10); //number to convert, string to save value in, base
buffer->Graphics->DrawString(number,Fuente,System::Drawing::Brushes::WhiteSmoke,50,50);,50);
I know how to generate random numbers, but what I really need is a string of random characters. This is what I have so far:
NSString *letters = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789?%$";
char generated;
generated = //how do i define this?
NSLog(#"generated =%c", generated);
[textField setStringValue:generated];
-(NSString*)generateRandomString:(int)num {
NSMutableString* string = [NSMutableString stringWithCapacity:num];
for (int i = 0; i < num; i++) {
[string appendFormat:#"%C", (unichar)('a' + arc4random_uniform(25))];
}
return string;
}
then Call it like this for a 5 letter string:
NSString* string = [self generateRandomString:5];
See this SO Q & A.
Generate a random alphanumeric string in Cocoa