Wednesday, September 12, 2012

bayoom.com - meet new people

To all you lonely developers out there ... ;o)
A new website just got released that is targeted at meeting new people.
You can search people in your area or somewhere else, chat, flirt, exchange pictures and do many more things.

As its new its not very crowded yet but will for sure grow fast in the upcoming days.
You can find it at:

http://www.bayoom.com

Be one of the first users there and gain maximum attention when the crowd raises!


You can register here:

http://www.bayoom.com/register

Or directly login with your facebook account here:

http://www.bayoom.com/fblogin





Thursday, April 12, 2012

Implement multi threaded / asynchronous delegate calls

When implementing own classes with a delegate protocol it is very simple to achieve an asynchronous behaviour. To explain how it works ill describe a simple scenario:

You have a class that searches big amount of text for keywords. This class should have a delegate protocol that calls following methods:

//this function is called whenever the class finds a string in the text and delivers an array containing page number, line and text excerpt
-(void)didFindTextOnPage:(NSArray*)arrData;
//function is called when search has finished
-(void)didFinishSearching;

When these methods are called in a different thread, on the main thread e.g. a UITableView could be continuously populated with the search results and a loader could be shown to notify the user that the search is still active. When the search is finished the loader can be removed.

All this is easily achieved with following snippet:

------------------------------------------------------------------


//start different thread
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   
   //do something in a different thread
   dispatch_async( dispatch_get_main_queue(), ^{
       //thread has finished so call final method
   });
});

------------------------------------------------------------------

In the first line we start a new thread. Everything within this "loop" wont affect the main thread.
dispatch_async... is the callback that is called when the "loop" has finished his operations.

In our search example the above code could be used as follows to have a multithreaded approach:

------------------------------------------------------------------

//start different thread
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
    //loop over pages
   for(int i = 0; i < [pages count]; i++){

      NSArray* arrLines = [NSDicionary objectAtIndex:i];

      //loop over lines
      for(int j = 0; j < [arrLines count]; j++){

         NSArray* arrLine = [arrLines objectAtIndex:j];

         //if keyword exists
         if([self keyWordExistsOnPage:i forLine:j] == YES){

            //call the delegate method
            [self.delegate didFindTextOnPage: arrLine];
         }
      }
    }

   //callback for thread
   dispatch_async( dispatch_get_main_queue(), ^{
      //thread has finished so call final method
      [self.delegate didFinishSearching];
   });
});


------------------------------------------------------------------

Hope this helps. Let me know in the comments if something could be improved here or if you have another approach.


Happy coding :)

Sunday, April 8, 2012

Saturday, April 7, 2012

iOS TTS (text-to-speech) offline and multilingual


I am currently working on a project where I should add text to speech functionlity.
I searched different blogs and forums and found several services that do TTS conversion via a webservice.

The problem is I need an offline library that can "speak" several languages. The only one I found was: http://espeak.sourceforge.net/
this lib is okay in the simulator but for some reason on the device (ipad 2 & 3) the speech is horrible and speaks out single umlauts and has very strange pronunciation...

Does somebody know a good solution for this problem (other lib or how to make eSpeak work correctly on the iPad)?

Let me know in the comments?

Thanks and happy coding :)

scrollable label / text


The last years i used a uiscrollview and added a label onto it. After that i calculated the size of the text height and did set the contentSize property of the scrollview. All this is not necessary by using a simple trick.

I wasn't aware of this before and maybe most people do it this way but in case somebody didn't know it here the very simple solution:

add a textview and set the editable property to no. That's it.

Happy coding :)

Saturday, December 18, 2010

Explanation of method headers in objective-c (for beginners)

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

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