ios


Subtracting numbers as Objects - Objective C


I have a sorted array, yrs, which is what it sounds like (a sorted array of years). This array holds 5 objects and each is similar to the object below:
__NSCFNumber * (int)1995 0x79fa3200
I'm trying to subtract the last item from the first item to get the date range:
int first_year = [yrs objectAtIndex:0];
int last_year = [yrs objectAtIndex:4];
NSInteger numberOfCols = ([last_year intValue] - [first_year intValue] ) + 1;
The values of the items in the array are as follows:
first_year int 2078365328 2078365328 where it should be 1995
last_year int 2083083520 2083083520 where it should be
numberOfCols NSInteger 328 328
I honestly have no idea what's going on here.
EDIT
NSMutableArray * years = [NSMutableArray array];
NSMutableArray * atts = [NSMutableArray array];
for(Treatment * treatment in items)
{
NSLog(#"%#",treatment.treatmentMolecule);
NSNumber * startYr = [NSNumber numberWithInt:treatment.startDate.yr];
NSNumber * endYr = [NSNumber numberWithInt:treatment.endDate.yr];
if((![years containsObject:startYr]) && (![startYr isEqual:#0])){
[years addObject:startYr];
}
if((![years containsObject:endYr]) && (![endYr isEqual:#0])){
[years addObject:endYr];
}
}
for(Attack * att in arrayAttack)
{
NSNumber * startYr = [NSNumber numberWithInt:att.yr];
if(![years containsObject:startYr])
[years addObject:startYr];
}
//sort yrs
yrs = [years sortedArrayUsingComparator:(NSComparator)^(NSNumber * yr1, NSNumber * yr2){
return [yr1 compare:yr2];
}];
This is wrong:
int first_year = [yrs objectAtIndex:0];
The element in the array is an instance of NSNumber. Try to replace it with this:
NSNumber *first_year = [yrs objectAtIndex:0];
Even better, you can use firstObject and lastObject instead of hard codes indices.
I would write it like this:
NSNumber *first_year = [yrs firstObject];
NSNumber *last_year = [yrs lastObject];
NSInteger numberOfCols = ([last_year integerValue] - [first_year integerValue] ) + 1;
You should do
int first_year = [[yrs objectAtIndex:0] intValue];
int last_year = [[yrs objectAtIndex:4] intValue];
to get the integer values saved in NSNumber.

Related Links

ios programatically change app icon during build process
Disable auto rotation for special method in swift?
How to hold queue execution until get response from web service API in Objective-C
How to set font sizes for different iPhone sizes? [closed]
Optimizing DrawRect: for a Snake sort of game
iOS 9 app lunch URL for 'Storage and iCloud Usage'
App Thinning Size Report reports totally different size then App Store size
Json Values Append Array but ViewDidLoad it seem Empty
Adding a Wizard style ViewController between two NavigationViewControllers in IOS
Run MQTT Broker in iOS
how to rotate image to a particular latitude and longitude in swift?
how to add json file in ActionRequestHandler safari extinction
How do I make a control on a reused custom UIView trigger actions only on the ViewController currently being viewed?
NSPredicate SUBQUERY with nested arrays iOS Objective C
What delegate method will be called on receiving a push notification if the app is in killed/Force closed (Not in background) state?
didReceiveMessage not called after unlocking the iPhone screen?

Categories

HOME
azure
printing
unit-testing
demandware
sharepoint-2013
orbeon
spring-roo
applet
weight
jqgrid
visual-studio-2012
wso2is
base
push
oracle-agile-plm
message
monaco-editor
javers
garmin
slowcheetah
abaqus
servlet-filters
cayley
line-breaks
ssms-2014
azure-redis-cache
sumifs
flexjs
asp.net-identity-2
bluebird
instance
json-c
magento2.0.2
swipe
machine-language
nib
data-science
rights-management
runge-kutta
response
otroslogviewer
psychopy
wso2iot
pac
permgen
sweet.js
jquery-ui-dialog
integer-programming
autofocus
opal
docker-swarm-mode
ewsjavaapi
vega-lite
smartbanner
gcc4.8
replaygain
libev
attributerouting
replicate
rails-console
mit-scheme
ng-annotate
duration
ota
okuma
clientid
pypdf
readline
uat
ubiquity
hypervisor
appv
netbsd
dhc
pegkit
xerces
pencilblue
textmatching
llvm-gcc
sigkill
android-tablelayout
spiceworks
bit.ly
prettyfaces
bessel-functions
private-methods
cpp-netlib
syndication-feed
dirpagination
azimuth
disk-io
validform
pydatalog
preon
glulookat
hresult
google-authorship
src
slimbox
icon-fonts
code-structure
c++builder-xe2
ruby-1.9.2
jtemplates
custom-tag
msn-messenger
development-machine
plumtree

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App