I'm Simply trying to change the text on a UILabel on a Friday between 4 and 6pm using the below code but the if statement is not being called? I am using a subclass of UILabel via Interface Builder.
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(targetMethod) userInfo:nil repeats:YES];
}
-(void)targetMethod {
NSLog(#"Called Timer");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE hh mm"];
NSDate *dt = [NSDate date];
NSString *dayAsString = [formatter stringFromDate:dt];
NSArray *array = [dayAsString componentsSeparatedByString: #""];
if([[array objectAtIndex:0] isEqualToString:#"Friday"] && [[array objectAtIndex:1] intValue]>=4 && [[array objectAtIndex:1] intValue]<=6 && [[array objectAtIndex:2] intValue]>=0 && [[array objectAtIndex:2] intValue] <=59)
{
self.scrollingLabel.text = #"On Air";
NSLog(#"On Air");
}
else
{
self.scrollingLabel.text = #"Off Air";
NSLog(#"Off Air");
}
}
Any help on this would be very much appreciated.
Do you want to split time with spaces string it?
NSArray *array = [dayAsString componentsSeparatedByString:#" "];
And "EEE" will return "Fri", not "Friday".
Also, if you need to manipulate date/time you can look at this library : DateTools
#import "DateTools.h"
NSDate *date = [NSDate date];
if (date.weekday == 6 && date.hour >= 16 && date.hour <= 18) {
NSLog(#"On Air");
}else{
NSLog(#"Off Air");
}
You have made 2 mistakes
[formatter setDateFormat:#"EEE hh mm"]; in this "EEE" will show only 3 characters of day like "Fri".
NSArray *array = [dayAsString componentsSeparatedByString: #""]; will return only one object in array.
Now you need to do
Either change "Friday" to "Fri" in you if condition or use "EEEE" (4E) that will return full name like "Friday".
Separate by single space in NSArray *array = [dayAsString componentsSeparatedByString: #" "];
setDateFormat:#"EEE hh mm"
With 3 E's in the date format, it returns short version of Weekday, like "Fri".
I think you need to change the comparison from "Friday" to "Fri"
Technically, your components are not being separated into individual elements at all. You forgot the space in the quotes after the 'componentsSeparatedByString' method. It should be:
NSArray *array = [dayAsString componentsSeparatedByString: #" "];
I tested it out, and the if statement should be called and return the specified output.
Also, the first element returns only the first three characters of the day, such as 'Tue' for Tuesday. Change Friday to 'Fri' and you should be good.
Related
So, I've got an sorted NSArray that contains NSString object (downloaded from a server), with the format: yyyy-MM-dd.
It's pretty much like this:
NSArray <NSString *> *dates = #[#"2017-06-25",
#"2017-06-26",
#"2017-06-27",
#"2017-06-28",
#"2017-06-30",
#"2017-07-01",
#"2017-07-02",
#"2017-07-03"];
So, today is 2017-06-29, and it's not in the array. How do I get the next nearest one? In this sample is 06-30, but it might be 07-01 if 06-30 doesn't exist...
Update
So people are asking me about what I've attempted to do. So it's like this (not very effective, but work)
Find if today is in the array (if yes, return)
Loop dates:
2.1 Convert dateString to date
2.2 Compare if date is greater than today => return if YES
If not found in step#2, return last object in dates array.
Actual code:
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"yyyy-MM-dd";
NSDate *today = [NSDate date];
NSUInteger index = [dates indexOfObject:[formatter stringFromDate:today]];
// Step 1
if (index == NSNotFound) {
// Step 2: Loop converted
NSInteger i = 0;
for (NSString *date in dates) {
// Step2.1: find the next nearest date's index
NSDate *convertedDate = [formmater dateFromString:date];
// Step2.2: Compare
if ([convertedDate intervalSinceDate:today] > 0) {
index = i;
break;
}
i++;
}
// Step 3: Still not found, index = last index
if (index == NSNotFound) index = i-1;
}
return dates[index];
This doesn't look so good because I might reload the dates array pretty much. Can I have a better solution?
Your algorithm is not bad, though your code doesn't appear to implement it (no sort?). If you'd like to improve it consider this:
First there is probably little point in doing a first scan to check for an exact match - that is potentially a linear search (implemented by indexOfObject:) through an unordered array, and if it fails you have to scan again for a close match, just do them at the same time.
Second there is no advantage in sorting, which is at best O(NlogN), as a linear search, O(N), will find you the answer you need.
Here is a sketch:
Convert the date you are searching for from NSString to NSDate, call it, say, target
Set bestMatch, an NSString to nil. Set bestDelta, an NSTimeInterval, to the maximum possible value DBL_MAX.
Iterate over your dates array:
3.1. Convert the string date to an NSDate, say date
3.2. Set delta to the difference between date and target
3.3. If delta is zero you have an exact match, return it
3.4. If delta is better than bestDelta, update bestDelta and bestMatch
After iteration bestMatch is the best match or nil if there wasn't one.
That is a single iteration, O(N), early return on exact match.
HTH
Please find the simplest solution for your problem. Updated solution based on sorting order!
We can use NSPredicate Block to solve.
static NSDateFormatter* formatter = nil;
static NSDate* today = nil;
// return an NSDate for a string given in yyyy-MM-dd
- (NSDate *)dateFromString:(NSString *)string {
if (formatter == nil) {
formatter = [NSDateFormatter new];
formatter.dateFormat = #"yyyy-MM-dd";
}
return [formatter dateFromString:string];
}
// Helps to return today date.
-(NSDate*) getTodayDate {
if (today == nil) {
today = [NSDate date];
}
return today;
}
// Helps to find nearest date from Array using Predicate
-(NSString*)findNearestDate:(NSArray*)dateArray {
today = nil;
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSString *dateString, NSDictionary *bind){
// this is the important part, lets get things in NSDate form so we can use them.
NSDate *dob = [self dateFromString:dateString];
NSComparisonResult result = [[self getTodayDate] compare:dob];
if (result == NSOrderedSame || result == NSOrderedAscending) {
return true;
}
return false;
}];
// Apply the predicate block.
NSArray *futureDates = [dateArray filteredArrayUsingPredicate:predicate];
if ([futureDates count] > 0) {
// Sort the Array.
futureDates = [futureDates sortedArrayUsingSelector: #selector(compare:)];
return [futureDates objectAtIndex:0];
}
return nil;
}
NSArray <NSString *> *dates = #[#"2017-06-25",
#"2017-06-26",
#"2017-06-27",
#"2017-06-28",
#"2017-06-30",
#"2017-07-01",
#"2017-07-02",
#"2017-07-03"];
NSLog(#"Nearest Date: %#", [self findNearestDate:dates]);
Answer: Nearest Date: 2017-06-30
1. Input
So you have an array of NSString like this
// input
NSArray<NSString *> * words = #[#"2017-06-25",
#"2017-06-26",
#"2017-06-27",
#"2017-06-28",
#"2017-06-30",
#"2017-07-01",
#"2017-07-02",
#"2017-07-03"];
2. Converting the array of NSString into an array of NSDate
First of all you need to convert the each input string into an NSDate
NSMutableArray<NSDate *> * dates = [NSMutableArray new];
NSDateFormatter * dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = #"yyyy-MM-dd";
for (NSString * word in words) {
[dates addObject:[dateFormatter dateFromString:word]];
}
3. Finding the nearestDate
Now you can find the nearest date
NSDate * nearestDate = nil;
NSTimeInterval deltaForNearesttDate = 0;
NSDate * now = [NSDate new];
for (NSDate * date in dates) {
NSTimeInterval delta = fabs([date timeIntervalSinceDate:now]);
if (nearestDate == nil || (delta < deltaForNearesttDate)) {
deltaForNearesttDate = delta;
nearestDate = date;
}
}
4. Conclusion
The result is into the nearestDate variable so
NSLog(#"%#", nearestDate);
Wed Jun 28 00:00:00 2017
How to convert Datetime timestamp to a NSDate?
How to make the inverse?
My method to convert datetime to a string :
+(NSString*) dateTojson:(NSDate*)date{
return [NSString stringWithFormat:#"/Date(%f)/",(double)([date dateWithTimeIntervalSince1970] * 1000)];
}
My inverse method:
+(NSDate*) jsonToDate:(NSString *)json
{
double milisec = 0;
json = [[[json stringByReplacingOccurrencesOfString:#"/Date(" withString:#""] stringByReplacingOccurrencesOfString:#"/" withString:#""] stringByReplacingOccurrencesOfString:#"-0200" withString:#""];
NSArray *arr = [json componentsSeparatedByString:#"-"];
for(NSString *s in arr) {
if(![s isEqualToString:#""]){
milisec += [s doubleValue];
}
}
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(milisec / 1000.0)];
return date;
}
When i use [self jsonToDate:#"/Date(1495497600)/"] where 1495497600 represents "05/23/2017", the method return me a wrong date (result = "01/18/1970").
Why?
Notes:
i'm not considering the time, only date.
My variable milisec is equals to 1495497600, so i think the problem is the method dateWithTimeIntervalSince1970.
already try some posts like:
Convert milliseconds to NSDate
How to Convert a milliseconds to nsdate in objective C
You don't really need to divide the milliseconds at the end:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:milisec];
Result:
2017-05-23 00:00:00 +0000
I'm trying to create month strings that look like "Jan", "Feb", "Mar"... Here is my code:
- (NSString *)getMonthNameString:(int)monthNumber {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"MMM"];
NSArray *monthNames = [formatter standaloneMonthSymbols];
NSString *monthName;
if (monthNumber > 0) {
return monthNames[monthNumber - 1];
}
return monthNames[1];
}
So if the month number is 1, I'm expecting the code to provide month name as "Jan" and if it is 2, it has to provide month name as "Feb" and so on. But the problem is that even though I have set the format as MMM, it is still creating month names of type "January", "February" etc instead of "Jan","Feb" etc. How do I sort this out?
Try:
-(NSString*)getMonthNameString:(int)monthNumber
{
NSDateFormatter *formate = [NSDateFormatter new];
[formate setDateFormat:#"MMM"];
NSArray *monthNames = [formate shortMonthSymbols];
NSString *monthName;
if (monthNumber > 0)
{
monthName = [monthNames objectAtIndex:(monthNumber - 1)];
}
return monthName;
}
That's usually not what a NSDateFormatter is for - it is for converting real dates, and not just month numbers.
If you want to stick with it, I suggest
-(NSString*)getMonthNameString:(int)monthNumber {
NSDateFormatter *formatter = [NSDateFormatter new];
return formatter.shortMonthSymbols[monthNumber-1];
}
I see no benefit in the extra check for > 0. This just masks programming errors. You might want to add an assertion to catch that during development. (Why should an invalid number return January anyway?)
Creating formatters is expensive, though - you might want to reuse the same instance over and over again.
Or just access an array directly, i.e.
-(NSString*)getMonthNameString:(int)monthNumber {
return #[#"Jan", #"Feb", ...][monthNumber-1]; // write up to December of course
}
standaloneMonthSymbols is a property of NSDateFormatter. I don't think it uses the dateFormat you've set. Try using shortStandaloneMonthSymbols property instead (or veryShortStandaloneMonthSymbols if you just need one letter symbol).
tahavath is right. Specifically, you want to use the shortStandaloneMonthSymbols property to get it to print "Jan" or "Feb" etc.
Try the following:
-(NSString*)getMonthNameString:(int)monthNumber
{
NSDateFormatter *formate = [NSDateFormatter new];
[formate setDateStyle:NSDateFormatterMediumStyle];
[formate setDateFormat:#"MMM"];
NSArray *monthNames = [formate standaloneMonthSymbols];
NSString *monthName;
if (monthNumber > 0)
{
monthName = [monthNames objectAtIndex:(monthNumber - 1)];
}
return monthName;
}
Check your code #Karuppu MGR
-(NSString*)getMonthNameString:(int)monthNumber
{
NSDateFormatter *formate = [NSDateFormatter new];
[formate setDateFormat:#"MMM"];
NSArray *monthNames = [formate standaloneMonthSymbols];
NSString *monthName;
if (monthNumber > 0 && monthNumber<13)
{
monthName = [monthNames objectAtIndex:(monthNumber - 1)];
// your process is right but , here you have attached "return value " so every time return the monthNames array value.
}
return monthName; // if you pass zero or greathan twelve monthName return nil value
}
I have a problem with NSDateFormatter dateFromString: method. There is an array of dictionaries with string dates and I want to convert them to NSDates. But there is a huge heap growth every time I call this method (I need to call it more than once, cause it's an "update" method). Also I call it in background thread and ARC is on. What am I doing wrong? Any help, please.
UPDATE:
Full code of the function:
- (GraphController *) initGraphWithData: (NSArray *) points forInterval: (GraphInterval) interval andFrame: (CGRect) frame gestures: (BOOL) gestures secondGraph: (NSArray *) secondPoints graphNames: (NSArray *) graphNames
{
self = [super init];
_calendar = [NSCalendar currentCalendar];
_secondPoints = secondPoints;
_graphNames = graphNames;
[self.view setFrame:frame];
_points = [[NSMutableArray alloc] init];
double minValue = HUGE_VALF, maxValue = -HUGE_VALF;
NSDate *minDate = [NSDate date], *maxDate = [NSDate dateWithTimeIntervalSince1970:0];
NSMutableDictionary *datesTable = [[NSMutableDictionary alloc] init];
int index = 0;
for(NSArray *pointArray in points){
NSDictionary *point = pointArray[0];
NSMutableDictionary *newPoint = [[NSMutableDictionary alloc] initWithDictionary:point];
// convert date
NSString *dateString = (NSString *)[point objectForKey:#"date"];
NSLog(#"dateString to convert: %#", dateString);
[_dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [_dateFormatter dateFromString: dateString];
[newPoint setObject: date forKey:#"date"];
// convert numbers
[newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[point objectForKey:#"value"]) floatValue]] forKey:#"value"];
if(secondPoints)
[newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[secondPoints[index] objectForKey:#"value"]) floatValue]] forKey:#"secondValue"];
[newPoint setObject: [NSNumber numberWithInt:index] forKey:#"index"];
// min and max
if([[newPoint objectForKey:#"value"] floatValue] < minValue)
minValue = [[newPoint objectForKey:#"value"] floatValue];
if([[newPoint objectForKey:#"value"] floatValue] > maxValue)
maxValue = [[newPoint objectForKey:#"value"] floatValue];
// check second value
if(self.secondPoints){
if([[newPoint objectForKey:#"secondValue"] floatValue] < minValue)
minValue = [[newPoint objectForKey:#"secondValue"] floatValue];
if([[newPoint objectForKey:#"secondValue"] floatValue] > maxValue)
maxValue = [[newPoint objectForKey:#"secondValue"] floatValue];
}
if([[newPoint objectForKey:#"date"] timeIntervalSince1970] > [maxDate timeIntervalSince1970])
maxDate = [newPoint objectForKey:#"date"];
if([[newPoint objectForKey:#"date"] timeIntervalSince1970] < [minDate timeIntervalSince1970])
minDate = [newPoint objectForKey:#"date"];
[self.points addObject:newPoint];
[datesTable setObject:newPoint forKey: [[NSNumber numberWithDouble:[date timeIntervalSince1970]] stringValue] ];
index++;
}
// set draw parameters
_drawVars = [[NSMutableDictionary alloc] init];
NSDate *startSearchDate;
switch (interval) {
case GraphIntervalWeek:
startSearchDate = [maxDate dateByAddingTimeInterval:-7*24*3600];
break;
case GraphIntervalMonth:
startSearchDate = [maxDate dateByAddingTimeInterval: -31*24*3600];
break;
case GraphIntervalSixMonths:
startSearchDate = [maxDate dateByAddingTimeInterval:-6*31*24*3600];
break;
case GraphIntervalYear:
startSearchDate = [maxDate dateByAddingTimeInterval:-365*24*3600];
break;
case GraphIntervalAllTime:
break;
default:
break;
}
NSMutableDictionary *searchPoint;
while(searchPoint == nil && [startSearchDate timeIntervalSince1970] > [minDate timeIntervalSince1970]){
searchPoint = [datesTable objectForKey:[[NSNumber numberWithDouble:[startSearchDate timeIntervalSince1970]] stringValue]];
startSearchDate = [startSearchDate dateByAddingTimeInterval:-24*3600];
}
if(searchPoint != nil && interval != GraphIntervalAllTime)
[self.drawVars setObject:[searchPoint objectForKey:#"index"] forKey:#"startDrawIndex"];
else
[self.drawVars setObject:[NSNumber numberWithInt:0] forKey:#"startDrawIndex"];
[self.drawVars setObject:[[NSNumber numberWithFloat:minValue] copy] forKey:#"minValue"];
[self.drawVars setObject:[[NSNumber numberWithFloat:maxValue] copy] forKey:#"maxValue"];
[self.drawVars setObject:minDate forKey:#"minDate"];
[self.drawVars setObject:maxDate forKey:#"maxDate"];
[self.drawVars setObject:datesTable forKey:#"datesTable"];
// set drawImageView
_drawArea = [[UIImageView alloc] initWithFrame: frame];
[self.view addSubview:self.drawArea];
// set overlayImageView for fingerInpect
_inspectOverlay = [[UIImageView alloc] initWithFrame:frame];
[self.view addSubview:self.inspectOverlay];
// set hintUIView for fingerInspect
_hint = [[Hint alloc] initWithFrame:frame];
[self.hint initHint];
self.hint.hidden = YES;
[self.drawArea addSubview:self.hint];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panHandler:)];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
if(gestures)
[self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:longPressRecognizer];
return self;
}
I see you are using Heapshot analysis. Good. Here are some more details:
http://www.friday.com/bbum/2010/10/17/when-is-a-leak-not-a-leak-using-heapshot-analysis-to-find-undesirable-memory-growth/
Now, hopefully, each one of your Heapshot iterations represents doing something in your app that should return it back to the base state. Opening and closing a document, for example.
If that is the case, your data is showing a 500K-600K growth per iteration. That is quite a bit.
Note that trying to analyze the last iteration is generally useless; many of those objects will exist because of whatever the current state of the app is. You really want to focus on that 2nd or 4th iteration.
As for why that specific date or date formatter is leaking, turn on reference count event tracking and then review the retain/release events for one of the leaked objects. There will be some number of extra retains.
Something you are doing right is init the NSDateFormatter before entering the loop. If you are expecting the same date format (MM/dd/yyyy) you can put in the same place you init it as well. It can be that you are allocating too many objects, so I could advise putting everything inside an #autoreleasepool {} (aka: the content of the loop inside the autorelease).
I am seeing you are allocating the *newPoint inside the for and then you are setting the date to that object. What do you do after?
A user enters a numerical string in a UILabel and the text is displayed as the user types.
NSString *input = [[sender titleLabel] text];
[display_ setText:[[display_ text] stringByAppendingString:input]];
This works fine and I format the display using NSNumberFormatter so that if 1000000 is entered it is converted to 1,000,000 upon tapping another button.
However, I'd like to get those grouping commas to be displayed as the user types. I can understand how to insert things into strings, but how to do it as the user types is not clear to me. Would this require a mutable string?
Maybe somehow monitor the string length and split it into groups of three and make and display a new string with the commas inserted? I could probably do that, but it is the "as it is typed" part that has me stymied.
Another thought is to append and display the string, then read the display into a new NSString and format it and display it again right away. So I tried that, and it almost works:
if (userIsEntering)
{
NSNumberFormatter *fmtr = [[NSNumberFormatter alloc] init];
[fmtr setNumberStyle:NSNumberFormatterDecimalStyle];
[fmtr setGroupingSeparator:#","];
[fmtr setDecimalSeparator:#"."];
NSString *out = [[display_ text] stringByAppendingString:digit];
NSNumber *num = [fmtr numberFromString:out];
NSString* formattedResult = [fmtr stringFromNumber:num];
[display_ setText: [NSString stringWithFormat:#"%#", formattedResult]];
[fmtr release];
}
And, along with the fact that the formatter is created and released with every digit entered, after 4 digits it returns null.
UPDATE: I figured out how to do it in a label (with some help from #Michael-Frederick). It uses an NSNotification.
This works perfectly for non-decimal numbers, but when I try to enter a decimal point it is ignored and removed. If I do not invoke this method, the decimal point is accepted and all works well.
Numeric entry is as follows (from a button):
NSString *digit = [[sender titleLabel] text];
if (userIsStillWorking_)
{
[display_ setText:[[display_ text] stringByAppendingString:digit]];
}
else
{
[display_ setText: digit];
userIsStillWorking_ = YES;
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateDisplay" object:nil];
And the updateDisplay method called by the notification is:
{
NSString *unformattedValue = [display_.text stringByReplacingOccurrencesOfString:
#"," withString:#""];
unformattedValue = [unformattedValue stringByReplacingOccurrencesOfString:
#"." withString:#""];
NSDecimalNumber *amount = [NSDecimalNumber decimalNumberWithString:unformattedValue];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setGroupingSeparator:#","];
[formatter setDecimalSeparator:#"."];
[display_ setText: [ formatter stringFromNumber:amount]];
[formatter release];
}
I've tried commenting out
unformattedValue = [unformattedValue stringByReplacingOccurrencesOfString:
#"." withString:#""];
but that makes no difference.
EDIT:
A user cannot type into a uilabel. You need to use either a uitextfield or a uitextview.
If you want to use a uitextfield, do something like this...
- (void) viewDidLoad {
[super viewDidLoad];
[textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void) textFieldDidChange:(UITextField *)textField {
NSString *unformattedValue = [textField.text stringByReplacingOccurrencesOfString:#"," withString:#""];
unformattedValue = [unformattedValue stringByReplacingOccurrencesOfString:#"." withString:#""];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:#","];
[formatter setDecimalSeparator:#"."];
NSNumber *amount = [NSNumber numberWithInteger:[unformattedValue intValue]];
textField.text = [formatter stringFromNumber:amount];
[formatter release];
}
Note that you are correct that NSNumberFormatter should be declared outside of the textFieldDidChange method. Note that this code would actually be for an integer. You could have to switch intValue to floatValue if need be. This code is untested, it is more of a general guide.
The best way to do this is to use 2 UIlabels. 1 of the labels is used to feed your NSNumberFormatter object by using [NSString stringByAppendingString:digit]; The other label is actually displayed. The trick is to set the label that is unformatted to hidden and the other label is set as an output for the number formatter. By feeding the hidden label to the number formatter, and outputting the displayed label from the number formatter, the number formatter should be set to the NSDecimalNumber style. Setting it all up this way, the result displayed is automatic commas while typing.