Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 7, 2012, 10:51 AM   #1
Mvkoe
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Objective-C / Java Question

Hey

So I'm learning Objective-C with a Java background. It turns out quite good atm. I'm dutch and learning it with a English book. Sometimes I'm not understanding things, but that's why i'm asking it here!

In Java you have a Default Constructor, and you can edit the constructor like in the example.

Code:
	public Date (int day, int month, int year){
		setYear(day);
		setMonth(month);
		setDay(year);
	}
How is this handeled by Objective-C, anyone has an example for this ?

Further I know you can't really use get in your Getter methods because Cocoa would argue sometimes.

Code:
public class Date {

	private int year;
	private int month;
	private int day;
	
	public Date (){
		
	}
	
	public Date (int day, int month, int year){
		setYear(day);
		setMonth(month);
		setDay(year);
	}
	
	public void setYear(int year){
		
	}
	
	public void setMonth(int month){
		
	}
	
	public void setDay(int day){
		
	}
	
	public int getYear(){
	
	}
	
	public int getMonth(){
	
	}
	
	public int getDay(){
	
	}
}
Mvkoe is offline   0 Reply With Quote
Old Dec 7, 2012, 11:49 AM   #2
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: San Diego, CA USA
Quote:
Originally Posted by Mvkoe View Post
In Java you have a Default Constructor, and you can edit the constructor like in the example.

Code:
	public Date (int day, int month, int year){
		setYear(day);
		setMonth(month);
		setDay(year);
	}
How is this handeled by Objective-C, anyone has an example for this ?
There's no real concept of a "constructor" in Obj-C. Instead, the standard is to create initialization routines. Something like:

Code:
-(void)initWithDay:(int)day withMonth:(int)month withYear:(int)year;

Date *d = [[Date alloc] initWithDay:1 withMonth:1 withYear:2013];
Quote:

Further I know you can't really use get in your Getter methods because Cocoa would argue sometimes.

Code:
	public void setYear(int year){	
	}
	
	public int getYear(){
	}
}
Use @property. It will generate this code for you automatically.
mfram is offline   0 Reply With Quote
Old Dec 8, 2012, 01:55 AM   #3
Mac_Max
macrumors 6502
 
Join Date: Mar 2004
You can also create custom getters and setters if you'd like:

Code:
 @property(getter=getFoo, setter=setFoo:) NSData *foo;
Mac_Max is offline   0 Reply With Quote
Old Dec 8, 2012, 08:27 AM   #4
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by Mac_Max View Post
You can also create custom getters and setters if you'd like:

Code:
 @property(getter=getFoo, setter=setFoo:) NSData *foo;
Or just skip properties altogether... There's no reason you can't just use setters and getters (but there's less boilerplate code involved if you use properties.)
ArtOfWarfare is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Noob Objective-C / Framework / AVFoundation question Golgafrincham Mac Programming 0 Jan 30, 2014 02:54 AM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
java to Objective-C spilakalb Alternatives to iOS and iOS Devices 0 Sep 30, 2012 11:17 PM
Objective C question farmerdoug Mac Programming 4 Sep 16, 2012 02:44 PM
Java question brentg33 Mac Applications and Mac App Store 1 Aug 28, 2012 06:45 PM

Forum Jump

All times are GMT -5. The time now is 12:15 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC