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.
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
google latitude in app store
Even this blogs main purpose is to write about development here and there ill post apps,technologies and tools i find especially usefull or cool....
Google latitude hit the app store this week and I must say its great. It took some time to come over from Android but it finally made it. I already felt in love with it on my old HTC Magic and am really happy about the look and feel on iOS.
Hope to see many more Google Apps on iOS Devices (even though they have there own OS) in the future...
Google latitude hit the app store this week and I must say its great. It took some time to come over from Android but it finally made it. I already felt in love with it on my old HTC Magic and am really happy about the look and feel on iOS.
Hope to see many more Google Apps on iOS Devices (even though they have there own OS) in the future...
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
Monday, December 13, 2010
For/Foreach -> php vs objective-c
Lets start with a short Post about for/foreach PHP vs Objective-C
To loop through an array in php is very simple:
foreach($arrData as $key => $value){
//do something...
}
or
for($i = 0; $i < count($arrData); $i++){
//do somethin...
}
In Objective-C you have similar possibilities but you have to be sure what kind of object you are dealing with e.g. NSDictionary* or NSArray*
To get an Element of these two types you do as follows:
NSDictionary
NSDictionary* arrData = ...;
[arrData objectForKey:@"id"];
Loop
for (NSString* key in arrData) {
id value = [arrData objectForKey:key];
}
NSArray
NSArray* arrData = ...;
[arrData objectAtIndex:1];
Loop
for (int i = 0; i < [arrData count]; i++) {
[arrData objectAtIndex:i];
}
Besides these options you can also use enumerateKeysAndObjectsWithOptions. But I personaly dislike it cause it "feels" bad.
If you have an array of similar objects and you want all of them to perform the same selector you can also do this:
[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
This will save you some lines of code and is very "readable".
Cheers
JayEs
To loop through an array in php is very simple:
foreach($arrData as $key => $value){
//do something...
}
or
for($i = 0; $i < count($arrData); $i++){
//do somethin...
}
In Objective-C you have similar possibilities but you have to be sure what kind of object you are dealing with e.g. NSDictionary* or NSArray*
To get an Element of these two types you do as follows:
NSDictionary
NSDictionary* arrData = ...;
[arrData objectForKey:@"id"];
Loop
for (NSString* key in arrData) {
id value = [arrData objectForKey:key];
}
NSArray
NSArray* arrData = ...;
[arrData objectAtIndex:1];
Loop
for (int i = 0; i < [arrData count]; i++) {
[arrData objectAtIndex:i];
}
Besides these options you can also use enumerateKeysAndObjectsWithOptions. But I personaly dislike it cause it "feels" bad.
If you have an array of similar objects and you want all of them to perform the same selector you can also do this:
[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
This will save you some lines of code and is very "readable".
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)