This is a short explanation for beginners coming from other languages like Java or PHP to show how classes are instantiated and how methods are called and defined.
To define a method in programming languages you usually have 3-4 Parts:
1.) Keyword to say that you are about to implement a method (e.g. function)
2.) What kind of object will the method return or void if nothing
3.) The method name
4.) Parameters
Lets say we have a class called calculator.
In this class we want to implement a method called increment. This method gets an integer and returns the integer incremented by one.
This method would look in php like this:
function increment($prmInt){
return $prmInt++;
}
In objective-c the same method would look like this:
-(int)increment:(int)intSomeNumber{
return intSomeNumber++;
}
"-" is equal to the word "function" and tells the compiler that a method starts
"(int)" defines what will be send back by the method (void if nothing)
"increment" this is the method name
":" tells the compiler that a parameter starts
"(int)intSomeNumber" type and name of parameter
If you want to create a method with several parameters you can just add ":" and some text at the end.
Some examples:
-(void)doSomething:(NSString*)strParameter1 andEvenMoreParameters:(NSString*)strParameter2;
-(void)aMethodWithoutTextbetweeenParameters:(int)i1:(int)i2:(int)i3;
To call these methods you have to instantiate the class:
Calculator* calc = [[Calculator alloc] init];
now you have allocated an object. With this object you can call the declared method:
int intNumber = 5;
intNumber = [calc increment:intNumber];
and for completeness' sake:
[calc doSomething:@"string1" andEvenMoreParameters:@"string2"];
[calc aMethodWithoutTextbetweeenParameters:1:2:3];
As we don't need the object anymore we will release it so that the memory gets freed:
[calc release];
Cheers
JayEs
Blog about development related topics like objective-c, cocoa, iOS, Android, Java and PHP.
Showing posts with label Cocoa. Show all posts
Showing posts with label Cocoa. Show all posts
Saturday, December 18, 2010
Friday, December 17, 2010
Asynchronous download/request with Objective-C
For handling asynchronous request there is a very neat and simple mechanism in Objective-C.
Delegates
The following header file defines a protocol for the delegate and the necessary methods and data types to create a downloader that constantly pushes an update about the download state to the main thread.
Header File
Implementation
This implementation is ment for big files like 200 MB zip files. If you just want to download small stuff like an image u can instead of writing into a file use a NSData object.
Hope this tutorial did help. Feel free to post some code improvements or questions in the comments?
P.S.: Will look for a nicer way to post code in blogger. For the moment I don't have a lot of time (child n.3 was born two days ago... ;o) ) so ill leave it this way.
Cheers,
JayEs
Here the code as Pastie-link:
header-file
implementation
Delegates
The following header file defines a protocol for the delegate and the necessary methods and data types to create a downloader that constantly pushes an update about the download state to the main thread.
Header File
Implementation
This implementation is ment for big files like 200 MB zip files. If you just want to download small stuff like an image u can instead of writing into a file use a NSData object.
Hope this tutorial did help. Feel free to post some code improvements or questions in the comments?
P.S.: Will look for a nicer way to post code in blogger. For the moment I don't have a lot of time (child n.3 was born two days ago... ;o) ) so ill leave it this way.
Cheers,
JayEs
Here the code as Pastie-link:
header-file
implementation
Tuesday, December 14, 2010
JSON in iPhone/iPad App
For you out there searching for a good JSON library for your iOS project this is the one Im using:
https://github.com/stig/json-framework/downloads
Howto use:
Very simple :-)
Cheers,
JayEs
https://github.com/stig/json-framework/downloads
Howto use:
- Add folder to your project
- Import the header file (#import "JSON.h")
- When you have a NSString containing JSON data simply use: [strSomeString JSONValue]; and you get an NSDictionary/NSArray
Very simple :-)
Cheers,
JayEs
Sunday, December 12, 2010
First Post
This is going to be a blog about dev and tech related topics.
The main topics regarding development will be:
The main topics regarding development will be:
- Objective-C / Cocoa
- PHP
- Java / Android
Subscribe to:
Posts (Atom)