I got a question for anyone with a spare moment and an answer...
I need to change an NSString value to input into the AVAudioPlayer start offset
eg. player.currentTime = 0; I want to change the '0' to a readable value or an NSString to a numeral value.....
NSString has a integerValue function. You can also create an NSString using it's format class function and pass it a format string like this: "%d" and then your player.currentTime.
int timeInput = [[textField text] integerValue];
label.text = [NSString stringWithFormat:#"%d", timeInput];
Related
This is the line of code in question:
bks.quantity = [NSNumber numberWithInteger: [[arrayOfSplitStrings[i] objectAtIndex:[[sd.dictionaryOfUserIndexes objectForKey: #"29"] intValue]] intValue]-1];
sd.dictionaryOfUserIndexes objectForKey: #"29" contains a string (representing a quantity) that has to be converted to NSNumber. When the statement is executed with a valid string quantity (0-10) it always returns -1, when it is supposed to return the NSNumber for the value.
What am I doing wrong?
This is not a "straight forward" answer (since the solution is just a silly one), it's more a suggestion on work methods, that's why I post an answer.
It's not always good to put it various lines in a single line.
Especially when in your case you encounter an issue. It's better to split each command, one by one, and to debug, check the value of each ones.
In your case:
bks.quantity = [NSNumber numberWithInteger: [[arrayOfSplitStrings[i] objectAtIndex:[[sd.dictionaryOfUserIndexes objectForKey: #"29"] intValue]] intValue]-1];
==>
NSInteger userOfIndexes = [[sd.dictionaryOfUserIndexes objectForKey: #"29"] intValue];
NSLog(#"userOfIndexes: %d", userOfIndexes);
NSInteger n = [arrayOfSplitStrings[i] objectAtIndex:userOfIndexes] intValue];
NSLog(#"n: %d", n);
bks.quantity = [NSNumberWithInteger:n-1];
I added NSLog(), but the values could be check with breakpoints and debugger. I could have also add a check on arrayOfSplitStrings with
NSArray *splitStrings = arrayOfSplitString[i];
NSLog(#"splitStrings: %#", splitStrings);
and replace n with:
NSInteger n = [splitStrings objectAtIndex:userOfIndexes] intValue];
That way, you would have check that apparently (according to your comment), your issue was were to put the "-1.
NSInteger n = [[arrayOfSplitStrings[i] objectAtIndex: userIndex-1] intValue];
Thanks in advance!
I'm wondering if it's possible to convert HTML code stored in a NSString to a NSData to parse later.
I'm reading from a BBDD the HTML code and saving it into a NSString.
NSString *htmlString =#"<html><body><p>introduccion</p><p>introducción</p></body></html>";
I want to use:
NSData *nsData = [[NSData alloc] initWithContentsOfURL:url];
But instead of initWithContentsOfURL i have to use the htmlString because I have the code stored in a BBDD and I am accessing it and storing the code to a NSString
Thanks!
Why dont you use standard methods to convert NSString to NSData like this:
NSData* data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
I have some html formatted text in my "subject.details", I add some more html formatting on the front and back. Giving me a "detailsStringForDisplay" NSString.
I convert this string to an NSData.
Set up a dictionary, which only contains one element to say that the document type is HTML.
Then finally set an attributedText using the data and the dictionary.
NSString *fontDetailsHTML = #"<div style=\"font-size:17px; text-family:Georgia; \">";
NSString *detailsString = subject.details;
NSString *endFontDetailsHTML = #"</div>";
NSString *detailsStringForDisplay = [NSString stringWithFormat:#"%#%#%#",fontDetailsHTML,detailsString,endFontDetailsHTML];
NSData *labelTextData = [detailsStringForDisplay dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:true];
NSDictionary *attributesForText = [[NSDictionary alloc]initWithObjectsAndKeys:NSHTMLTextDocumentType,NSDocumentTypeDocumentAttribute , nil];
self.detailDescriptionLabel.attributedText = [[NSAttributedString alloc]initWithData:labelTextData options:attributesForText documentAttributes:nil error:nil];
I am trying to add the value of my textfield plus the value of my label and place it in another label.
-(IBAction)finalTipTotalSelection:(id)sender
{
NSString *billFinalAmount = [billAmountTextField text];
float billFinalAmountFloat = [billFinalAmount floatValue];
NSString *tipTotalAmount = [resultLabel text];
float tipTotalAmountFloat = [tipTotalAmount floatValue];
float finalAmountShow = billFinalAmountFloat + tipTotalAmountFloat;
NSString *finalResult = [NSString stringWithFormat:#"$ %0.2f", finalAmountShow];
[finalTotalLabel setText:finalResult];
}
I am creating floatValues for the Strings and then in a float adding the other floatValues together, and finally displaying it in a label. The only issues is when I run this code the finalTotalLabel (shows the final amount) only shows the value of the billAmountTextField. So if the billAmountTextField = 187.82 and the tipTotalAmount was 10, the finalTotalLabel would show 187.82 (not 197.82). I cant seem to find my mistake. Thanks for your help!
It is caused by the tipTotalAmount having the $ character at the beginning of the string. The resulting float value for the tip amount will be 0.0 because:
floatValue... returns 0.0 if the receiver doesn’t begin with a valid
text representation of a floating-point number.
You can take a look at the class reference of NSString for the explanation of the return value of floatValue.
Try to filter out the non decimal and dot character first from the NSString instance before passing the message floatValue to it, for example:
NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString:#"."];
[_alnum formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
NSString *newTipTotalAmount = [[tipTotalAmount componentsSeparatedByCharactersInSet:
[_alnum invertedSet]]
componentsJoinedByString:#""];
float tipTotalAmountFloat = [newTipTotalAmount floatValue];
Here the newTipTotalAmount will be "39.19" compared to the tipTotalAmount which is "$ 39.19" and passing the floatValue message to newTipTotalAmount will give you the correct float value.
Now I am trying to add a front end check on my app to detect if user input only number in the textfield.
I use:
- (IBAction)checkID:(UITextField *)sender {
if ([sender.text isEqualToString:#""]) {
sender.text = #"This information is required";
sender.backgroundColor =[UIColor redColor];
}else if (![sender.text intValue]) {
sender.text = [sender.text stringByAppendingString:#" is not valid number"];
sender.backgroundColor =[UIColor redColor];
}
NSLog(#"send.text is %#, intValue is %d",sender.text,[sender.text intValue]);
}
But I found it text begins with number and ends with string, its intValue is still the number.
In my text, text is "00001aa", but the intValue is 1.
Is there any other way to filter out this "00001aa" text?
Thanks in advance.
Yes, you can use NSRegularExpression, or NSCharacterSet (works for positive numbers).
For regular expressions, use ^[-+]?[0-9]+$.
NSRegularExpression *numEx = [NSRegularExpression
regularExpressionWithPattern:#"^[-+]?[0-9]+$" options:0 error:nil
];
NSLog(#"%ld", [numEx numberOfMatchesInString:#"-200" options:0 range:NSMakeRange(0, 4)]);
NSLog(#"%ld", [numEx numberOfMatchesInString:#"001A" options:0 range:NSMakeRange(0, 4)]);
For character set, use [NSCharacterSet decimalDigitCharacterSet].
BOOL isNum = [[NSCharacterSet decimalDigitCharacterSet]
isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:#"001AA"]
];
I'm trying to find out how to go through my CoreData information and find objects that have a createdAt (part of my object as an NSDate) that is within a NSTimeInterval. How do I set this up?
I've looked on the documentation at:
http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/predicates.html
But I'm not finding anything there.
Do I need to create two time stamps and use SQL's BETWEEN?
Any help would be wonderful.
First of all, it doesn't make sense to check if an NSDate is within an NSTimeInterval, because NSTimeInterval just specifies a length of time, not its location. Instead, you want to use two separate NSDates specifying the beginning and end of your intervals.
Here's what it would look like (beginningTime and endTime are NSDates).
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:#"YourEntityName" inManagedObjectContext:yourContext];
NSPredicate *beginningPredicate = [NSPredicate predicateWithFormat:#"createdAt >= %#", beginningTime];
NSPredicate *endPredicate = [NSPredicate predicateWithFormat:#"createdAt <= %#", endTime];
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:beginningPredicate, endPredicate, nil]];
NSArray *results = [yourContext executeFetchRequest:request error:NULL];