How enter in '.' first and then number in text of texfield? [closed] - iphone

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want enter float value in text field like as '.35'or '35' but not as '0..3678'.I want to restrict on enter of double dots. How do that?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber* myNumber;
NSString* myString = [textField.text stringByReplacingCharactersInRange:range withString:string];
range = NSMakeRange(0, [myString length]);
[numberFormatter getObjectValue:&myNumber forString:myString range:&range error:nil];
if (([myString length] > 0) && (myNumber== nil || range.length < [myString length])) {
return NO;
}else {
return YES;
}
}
I use above code for enter numeric value but it can't enter first dot/point and then number. What is error in above function?

int dots = 0;
for (int i = 0; i<[string length]; i++) {
char test = [string characterAtIndex:i];
if (test == '.') dots++;
}
if (dots > 1) {
return NO;
}
return YES;

Just check if the character is the first character, check if it is a dot, and if it is, pass 0. as the initial two characters in myString.

Related

how to convert NSMutable array with NSNumber into strings [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
my array is containing ids they are in NSNumber how can i convert them in string my array is like below
1494447926,
1537064431,
1545735176,
1574825141,
1604834983,
1829486110,
1838260338,
1846543841,
1850381039,
100000039842949,
100000077723868,
100000103091995,
100000126558358,
100000130915431,
100000139092102,
100000157330187,
100000157646688,
100000197141710,
100000243178639,
100000249947961,
please give me sample code to convert it to string
First of all array can not store integer. It must be in NSNumber or it is in NSString itself.
In either of the case you can create a long string by appending them,
NSString *string=[yourArray componentsJoinedByString:#","];
Or, if you want each value as string then you need to create that much string and then access them.
NSArray *numbersToStrings=[NSArray new];
for(id element in yourArray){
[numbersToStrings addObject:[NSString stringWithFormat:#"%#",element];
}
Here numbersToStrings contains all the values as string.
Use this.
NSString *str = [NSString stringWithFormat:#"%i",number];
for(int i=0;i<[arr count];i++){
str = [NSString stringWithFormat:#"%d",[arr objectAtIndex:i]];
[newArr addObject:str];
}
NSString *str = [NSString StringWithFormat:#"%d",1494447926];
You can use stringWithFormat
NSString *str = [NSString stringWithFormat:#"%d", [YourArray objectAtIndex:index]];
You cannot store integers into an array. If you are getting this response from server each would be NSNumber. You can type cast that to NSString.
do this
NSArray *ll=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSString *strinList=[NSString stringWithFormat:#"%#",[ll objectAtIndex:0]];
try this ,if you required other help ,i am here .
Only Search on Google - convert int to NSString , multiple Answer are displayed
by the way, your need to use only [NSString stringWithFormat:#"%d",YourIntValue]
for (int i=0; i < MyArray.count; i++)
{
NSString * String =[NSString stringWithFormat:#"%d", [MyArray objectAtIndex:i]];
NSLog(#"%#",String);
}

I want to sort my array in ascending order [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Array: (2007-99 , 2001-96, 2005-93)
Sorted Output should be: (2005-93, 2001-96, 2007-99)
Please help me out.
You need to write a custom comparator to do something like this. In the method below, I get the location of the dash with rangeOfString, then get the substring starting 1 position further into the string, then convert that to an int to do the comparison:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#"2007-07",#"2005-01",#"2004-09",#"2003-02", nil];
NSArray *sortedArray = [arr sortedArrayUsingComparator: ^(NSString *s1, NSString *s2) {
if ([[s1 substringFromIndex:[s1 rangeOfString:#"-"].location + 1] intValue] > [[s2 substringFromIndex:[s2 rangeOfString:#"-"].location + 1] intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[s1 substringFromIndex:[s1 rangeOfString:#"-"].location + 1] intValue] < [[s2 substringFromIndex:[s2 rangeOfString:#"-"].location + 1] intValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(#"%#",sortedArray);
You can sort this using a custom block (note that I assume that all of your numbers are formatted correctly):
NSArray *rollNumbers = [NSArray arrayWithObjects:#"2007-99", #"2001-96", #"2005-93", nil];
NSArray *sortedRollNumbers = [rollNumbers sortedArrayUsingComparator:^NSComparisonResult(NSString *roll1, NSString *roll2) {
NSArray *roll1Components = [roll1 componentsSeparatedByString:#"-"];
NSArray *roll2Components = [roll2 componentsSeparatedByString:#"-"];
NSNumber *roll1Number = [NSNumber numberWithInt:[[roll1Components objectAtIndex:1] intValue]];
NSNumber *roll2Number = [NSNumber numberWithInt:[[roll2Components objectAtIndex:1] intValue]];
return [roll1Number compare:roll2Number];
}];
NSLog(#"%#", sortedRollNumbers);
Output:
(
"2005-93",
"2001-96",
"2007-99" )
You can sort your array like this :
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#"2007-07",#"2005-01",#"2004-09",#"2003-02", nil];
NSMutableArray *marks = [[NSMutableArray alloc]init];
for (int i = 0; i < arr.count; i++)
{
NSArray *sep = [[arr objectAtIndex:i] componentsSeparatedByString:#"-"];
[marks addObject:[sep objectAtIndex:1]];
}
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:#"" ascending:NO];
[marks sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
NSMutableArray *sortedFinalArray = [[NSMutableArray alloc]init];
for (int i = 0; i < marks.count; i++)
{
for (int k = 0; k < arr.count; k++)
{
NSRange aRange = [[arr objectAtIndex:i] rangeOfString:[marks objectAtIndex:k]];
if (!(aRange.location == NSNotFound))
{
[sortedFinalArray addObject:[arr objectAtIndex:k]];
}
}
}
In order that you can sort your array, the elements of the array have to be compared pairwise to find out their ordering. Your specific ordering is custom, so you have to write a compare method (e.g. named compare:) by yourself, and then you can use [arr sortUsingSelector:#selector(compare:)]; to sort your array.
Now the compare: method has to be known to the elements of the array, because each element uses it to compare it to another element of the same class. So either you define a new class for your elements that implements the compare method, or you leave them as NSStrings, buth the you have to define a category that implements the compare: method.
The compare: method itself could look like this (pseudo code):
-(NSComparisonResult) compare: (MyString *) myString {
if
(self.the_last_2_characters_interpreted_asNumber <
myString.the_last_2_characters_interpreted_asNumber)
return NSOrderedAscending;
else if
(self.the_last_2_characters_interpreted_asNumber ==
myString.the_last_2_characters_interpreted_asNumber)
return NSOrderedSame;
else
return NSOrderedDescending;
}

How can i check the array has object or not [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i want to check if the old array has object or not if the old array has the object it should show me the button if the oldArray has zero object the button should be hidden the code is given below thanks...
-(void)viewWillAppear:(BOOL)animated
{
GET_DEFAULTS
NSMutableArray *array = [defaults objectForKey:kShouldResume];
NSData *dataRepresentingSavedArray = [defaults objectForKey:kShouldResume];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
{
array = [[NSMutableArray alloc] initWithArray:oldSavedArray];
if ([oldSavedArray containsObject])
{
btnResumeGame.hidden=NO;
}
else
{
btnResumeGame.hidden=YES;
}
}
else
{
array = [[NSMutableArray alloc] init];
}
}
}
Array has property count.
You can check weather count is zero or more than that as you require..
like
oldSavedArray.count
use this code:
if ( [oldSavedArray count]>0 ){
btnResumeGame.hidden=NO;
}
else{
btnResumeGame.hidden=YES;
}

Transforming NSMutableArray values [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I've been looking for this, but can't find the answer.
I have an NSMutableArray with values who_1, what_2, where_3 etc.
I want to transform this into who, what, where etc.
I already have the value of the integer as a variable, and _ is just a string.
What steps should I take to have all these arrayvalues transformed?
NSArray * arrB = [[NSArray alloc]initWithObjects:#"apple_a",#"ball_b",#"cat_c",#"doll_d",nil];
NSMutableArray * arrA = [[NSMutableArray alloc]init];
for(NSString *strData in arrB)
{
NSArray *arr = [strData componentsSeparatedByString:#"_"];
[arrA addObject:[arr objectAtIndex:0]];
}
and this would be your output
arrA:(
apple,
ball,
cat,
doll
)
You need to apply logic for that, You cant find answers to tricky Questions :)
You need to run a loop.
Separate string with '_'
Loop
for(NSString *s in ary)
{
NSArray *a = [s componentsSeparatedByString:#"_"];
[anotherArray addObject:[a objectAtIndex:0]];
}
and update your array..
Following might help you -
NSRange range = [string rangeOfString:#"_"];
NSString *finalString = [originalString substringToIndex:range.location];
you can have this in loop.
Or you can go for componentSeperatedByStrings.
This might help you
NSMutableArray *tmpAry = [[NSMutableArray alloc] init];
for(NSString *_string in _StringAry)
{
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"_0123456789"];
_string = [[_string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:#""];
[tmpAry addObject: [[_string copy] autorelease]];
}
NSLog(#"%#", tmpAry); // Gives the modified array
[tmpAry release];

How to remove the junk characters from string in aes128 decryption [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The problem is encryption in server side they are using ISO10126d2Padding and decryption is happening but after decryption it is showing some junk values anyone please help remove that :
ACTUAL RESULT = india ismy
*DECRYPTED VALUE = India ismyg~²t
I will not say that this is the best way to remove unwanted characters from NSString but i did this ... and it is great.
NSString * str = #"your string";
NSMutableString * newString;
int j = [str length];
for (int i=0; i<j; i++) {
if (([str characterAtIndex:i] >=65 && [str characterAtIndex:i] <=90) || ([str characterAtIndex:i] >=97 && [str characterAtIndex:i] <=122) ||[str characterAtIndex:i] == 32 ) {
[newString appendFormat:#"%c",[str characterAtIndex:i]];
}
}
//([str characterAtIndex:i] >=65 && [str characterAtIndex:i] <=90) this is ASCII limit for A-Z
//([str characterAtIndex:i] >=97 && [str characterAtIndex:i] <=122) this is ASCII limit for a-z
//and [str characterAtIndex:i] == 32 is for space.
Now, print new string
NSLog(#"%#",newString);
let me know if it is woking for you!
Thank You!