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

Adding Delay In A For Loop Without Blocking UI
Swift 1/2 UITextView suggestion by tag “#” or “#”
UICollectionViewController that looks like UITableViewController
iPhone simulator screenshot is not in proper dimension
How to update app via Xcode 5.1.1 for iOS 9
How to load txt file to UITextView with attributedString?
How to track In-App Refund in ios app [duplicate]
Displaying custom keyboard build with storyboard on iOS
“Core Data could not fulfill a fault for…”
Swift Optional Type not Unwrapped
UIScrollView Height and width
iOS - SFSafariViewController Freezes/Hanging
MTAudioProcessingTap - produce more output samples?
Localizable file isn't working on device
Parse email verification error with Swift 2
How to change Device Identity Certificate(SCEP certificate)'s expiration date in OS X Server App(for Profile Manager)

Categories

HOME
apacheds
quickblox
runtime-error
middleware
streaming
format
ionic3
sony-camera-api
build.gradle
jfreechart
add-in
autofac
stm
zebble
cobol
higher-order-functions
cytoscape.js
scale
rgb
factory-girl
amadeus
angular-components
jquery-selectors
graphcool
gas
pwm
polynomials
wso2cep
jsgrid
google-oauth2
expression-trees
clappr
concurrenthashmap
udid
skyscanner
subtotal
lifecycle
advanced-installer
google-crawlers
ubuntu-server
msal
json-rpc
ecdsa
sinon
extjs4
uitabbarcontroller
dxgi
automata-theory
project-centennial
spark-submit
backwards-compatibility
program-slicing
php-deployer
hostapd
soql
dwr
hipi
glumpy
facebook-canvas
waf
lapply
atmosphere.js
mailcore2
perspective
olingo
kernighan-and-ritchie
sql-server-2016-express
nuitka
restbed
app-engine-ndb
ota
flotr2
viewengine
rabbitvcs
js-of-ocaml
xelement
membership
asp.net-authorization
armv7
python-dragonfly
par
wchar-t
nominatim
ppl
sketching
iphone-5
cordova-cli
getuikit
fortrabbit
epplus-4
mks
slick2d
physx
mpj-express
eventmachine
data-quality
sirtrevor
asciiencoding
notifyicon
data-dictionary
sshj
libavcodec
execcommand
ioerror
emitmapper
datasift-python
programmers-notepad
sparc
python-dateutil
neventstore
northwind
icon-fonts
wescheme
browserid
timestamp-with-timezone
arr
ekeventkit
cellid
obout
self-organizing-maps
expression-encoder-sdk
c++builder-xe2
azure-role-environment
livevalidation
syncml
xoom
high-traffic
sharepoint-feature
crc-cards
variable-names

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