"Format string is not string literal (potentially insecure)" [duplicate] - ios

This question already has answers here:
Format String is not a literal string (potentially insecure) warning [duplicate]
(1 answer)
Warning : Format string is not a string literal (potentially insecure)
(1 answer)
Closed 8 years ago.
I'm creating a school app that displays the messages, users and the timestamp. I tried looking online for a fix to this but i couldn't find anything that I could understand. I'm new to programming and anything could help. I read somewhere that NSLog could help but I don't know how it's used. Please help!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"messagesCell";
MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
reversedMessages = [[messagesArray reverseObjectEnumerator] allObjects];
PFObject *tempObject = [reversedMessages objectAtIndex:indexPath.row];
cell.cellTitle.text = [tempObject objectForKey:#"content"];
cell.userName.text = [tempObject objectForKey:#"user"];
NSDate *createdAt = [tempObject createdAt];
NSDateFormatter *dateDisplay = [[NSDateFormatter alloc] init];
[dateDisplay setDateFormat:#"MMM, d h:mm a"];
**cell.timeStamp.text = [NSString stringWithFormat: [dateDisplay stringFromDate:createdAt]];**
return cell;
}

If you're using stringWithFormat, it expects a format string with %#'s being replaced by comma-separated parameters, like this:
[NSString stringWithFormat:#"This is param1: %#, and param2: %#", param1, param2];
If you're using a normal NSString, you want to simply do this:
cell.timeStamp.text = [dateDisplay stringFromDate:createdAt];
The alternative, which I want to doubly emphasize is just to contrast with the better way above and in this case is pointless, would be:
cell.timeStamp.text = [NSString stringWithFormat:#"%#", [dateDisplay stringFromDate:createdAt]];

Related

YapDatabase sorting confounds editing update

In a UITableViewController I use YapDatabase with Mantle sorting the following way:
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(NSString *group, NSString *collection1, NSString *key1, XQBuilding *object1, NSString *collection2, NSString *key2, XQBuilding *object2) {
if ([group isEqualToString:XQBuildingsViewGroupName]) {
return [[object1 name] compare:[object2 name] options:NSNumericSearch];
}
if ([group isEqualToString:XQPicturesGroup]) {
return [[object1 updatedAt] compare:[object2 updatedAt]];
}
return NSOrderedSame;
}];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = NO;
YapDatabaseView *databaseView = [[YapDatabaseView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:#"" options:options];
Although the used option I have sometimes (when an edited name changes the edited item order in the list) incorrect indexPath on reading:
- (UITableViewCell*)editableTableView:(UITableView *)tableView simpleCellForIndexPath:(NSIndexPath *)indexPath
{
__block XQBuilding *building;
__block NSNumber *gla;
[self.readConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
building = [[transaction ext:XQBuildingListYapName] objectAtIndexPath:indexPath withMappings:self.tableViewAnimator.viewMappings];
gla = [building glaWithTransaction:transaction];
}];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BuildingCell"];
cell.textLabel.text = building.name;
cell.detailTextLabel.text = [NSString stringWithFormat: #"%# sqm", formatDecimal(gla)];
return cell;
}
i. e. building in such case is a different one than was edited. How to get a correct indexPath according to actual sorting?
There are plenty of things that should be correctly coded to make it work properly!
As I can see from your code you use YapDatabaseViewMappings. Do you use LongLivedReadTransactions and do you subscribe to YapDatabaseModifiedNotification to properly modify the table on database changes?
There is an simplified example that shows how to use all these things to update your UITableView in real time right after database is updated.

How to get TimeZone abbreviation

I want to create a list of time zones.
But I got the following error at timeZone.abbreviation.
-[__NSCFString abbreviation]: unrecognized selector sent to instance 0x19cb80b0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
// Configure the cell...
NSTimeZone *timeZone = [[NSTimeZone knownTimeZoneNames] objectAtIndex:indexPath.row];
cell.textLabel.text = timeZone.abbreviation; // <- Error Here
cell.detailTextLabel.text = timeZone.description;
cell.accessoryType = (timeZone == self.timeZone)? UITableViewCellAccessoryCheckmark :UITableViewCellAccessoryNone;
return cell;
}
I tried to search on the internet but I cannot find the solution so far.
Please give me some advice.
Thanks in advance.
Try something like this:
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:[[NSTimeZone knownTimeZoneNames] objectAtIndex:indexPath.row]];
cell.textLabel.text = timeZone.abbreviation; // <- Error Here
cell.detailTextLabel.text = timeZone.description;
Your current code gets an array of NSStrings, not NSTimeZones. This should give you an NSTimeZone, and then you can get the abbreviation property on it.
as mentioned here: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimeZone_Class/Reference/Reference.html#//apple_ref/occ/clm/NSTimeZone/knownTimeZoneNames
[NSTimeZone knownTimeZoneNames]
returns a list of String ids of the timezones - so your timezone object actually contains a NSString not a NSTimeZone object. So you have to do:
NSString *timeZoneID = [[NSTimeZone knownTimeZoneNames] objectAtIndex:indexPath.row];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneID];
to get the real timezone object to call abbreviation on

Multiple methods named 'location' found with mismatched result, parameter type, or attributes

I have read the other questions concerning multiple methods but still do not know how to fix my code. I would be grateful for help with this. I have put a * around the statement where the error occurs.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"eventCell";
EQCalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[EQCalendarCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.titleLabel.text = [[self.eventsList objectAtIndex:indexPath.row] title];
cell.locationLabel.text = [[self.eventsList objectAtIndex:indexPath.row] location];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat: #"dd-MM-yy HH:mm"];
NSString *startDateString = [df stringFromDate:[[self.eventsList
objectAtIndex:indexPath.row] startDate]];
cell.startDateLabel.text = startDateString;
NSString *endDateString = [df stringFromDate:[[self.eventsList
objectAtIndex:indexPath.row] endDate]];
cell.endDateLabel.text = endDateString;
return cell;
}
Thanking you in advance for your help.
Casting the result of retrieving the object from the self.eventsList collection should solve the problem. E.g.:
cell.locationLabel.text = [((MyClassName *)[self.eventsList objectAtIndex:indexPath.row]) location];
Replacing MyClassName with the name of the class in the collection.
You need to cast [self.eventsList objectAtIndex:indexPath.row] to the relevant type so that the compiler knows what data type your are dealing with.
Without seeing how your self.eventList list is populated it's not possible to tell you the solution exactly, but your line should be replaced with something like this (split into two lines for clarity, but you could use a cast instead of a variable to keep it on one line)
MyEventClass *event = [self.eventsList objectAtIndex:indexPath.row];
cell.locationLabel.text = [event location];
You need to cast EKEvent class. This will solve your problem...
EKEvent *event = [self.eventlist objectAtIndex:indexPath.row];
NSString *placeStr=[event location];
In one scenario, if the factory method that creates the object has return type "id" then the compiler will check the method signature in all the classes. If compiler find the same method signature in more than one class then it will raise the issue. So replace the return type "id" with "specific class name".

Extracting from the right of a string in objective C [duplicate]

This question already has answers here:
Get last 2 characters of a string?
(2 answers)
Closed 9 years ago.
This seems to be what I'm looking for but in reverse. I would like the string to extract from the right not from the left.
The example extracting from the left is given:
NSString *source = #"0123456789";
NSString *firstFour = [source substringToIndex:4];
Output: "0123"
I'm looking for a version of the below that works from the right (what is below doesn't work)
NSString *source = #"0123456789";
NSString *lastFour = [source substringToIndex:-4];
Output: "6789"
the [source substringFromIndex:6]; won't work because sometimes I will get an answer that is 000123456789 or 456789 or 6789. In all cases I just need the last 4 characters from the string so that I can convert it to a number.
there must be a better way than a bunch of if else statements?
As you are not sure, about the length of the string, so you must check it before extracting like this:
NSString *source = #"0123456789";
NSNumber *number;
if (source.length>=4) {
NSString *lastFour=[source substringFromIndex:source.length-4];
number=#([lastFour integerValue]); //and save it in a number, it can be int or NSInteger as per your need
}
NSLog(#"%#",number);
Also if you want a quick method that you need to call several times, create a category :
#implementation NSString (SubstringFromRight)
-(NSString *)substringFromRight:(NSUInteger)from{
if (self.length<from) {
return nil;
}
return [self substringFromIndex:self.length-from];
}
#end
And use it as :NSLog(#"%#",[source1 substringFromRight:4]);
NSString *source = #"0123456789";
NSString *newString = [source substringFromIndex:[source length] - 4];
NSLog(#"%#",newString);
replace
NSString *lastFour = [source substringToIndex:-4];
with
NSString *lastFour = [source substringFromIndex:[source length] - 4];
which returns you the last 4 characters of your original string string in lastFour string.
You can use the following code to get last 4 characters from your string.
NSString *last4Characters = [source substringFromIndex:(source.length - 4)];
NSLog(#"Last 4 Characters:%#",last4Characters);
last4Characters=nil;
Please let me know if any issue.

A better approach to coding the a string value test for a NSDictionary object [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How could I write this if else code checking in a better way?
My conditional code here seems repetitive and long. Is there a better approach? I want to test for a string value in a NSDictionary object and then depending upon the value prefix a UILabel with $, £, ¥ currency symbols.
Here's my code (I've just shown 2 examples below, I have more currencies and the code is very long):
if ([[item objectForKey:#"currency"] isEqualToString:#"EUR"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"€%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
if ([[item objectForKey:#"currency"] isEqualToString:#"GBP"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"€%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
if ([[item objectForKey:#"currency"] isEqualToString:#"USD"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"$%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
thanks so much for any help.
You can refactor your code to this (possible because essentially 80% of the code within the if statements are identical):
NSDictionary *currDict = #{
#"EUR": #"€",
#"GBP": #"₤",
#"USD": #"$"
};
NSString *currName = [item objectForKey:#"currency"];
NSString *currency = [currDict objectForKey:currName];
NSString *priceConvertToStr = [NSString stringWithFormat:#"%#%#",
currency,
[[item objectForKey:#"price"] stringValue]
];
NSString *priceStringFix = [priceConvertToStr stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];

Resources