Mindmajix

iPhone : Exploring the Foundation Framework – NS Thread & Number Class

NS thread:-

An NSThread object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application, which handles user interface and event-related actions. Threads can also be used to divide a large job into several smaller jobs, which can lead to performance increases on multi-core computers. (Refer to Exploring the Foundation Framework to know various class types)

 (void) view Did load
{
[NSThread detachNewThreadSelector:
 @ Selector  (blackGroundThread) to Target :self WithObjects  : nil];
For (int :=0; i<100; i++)
NSLog (@ “ mainThread”);

// sleep made for some seconds:

[NSThread SleepForTimeInterval:2];
NSLog (@ “the thread is sleep”,);

// getting current thread:

NSThread * currentThread= [NSThread CurrentThread];
NSLog (@ “the Current thread is %@”,  current Thread)];

// prepare thread in another way:-

    [ self PerformSelectorInBackground:
     @ selector ( backGround thread) WithObject : nil];
}
-(void) back ground thread
{
     For (int i=0; i<100; i++)
NSLog (@ “ child Thread”);
}

Multi threading:-

     In most of the modern OS, it is possible for an application to split into many threads that all execute concurrently.

     When a program is split into many threads, each thread acts like its own individual program except that all the threads work in the same memory space.

     Multi threads can run on multiple CPUS providing performance and improvement. A multi thread application work as well as sign single CPU system, but with added speed in objective C, there is a special class for threading i.e. NSThread class.

Ns thread:-

       If you need to run relative task on the back ground of the application after same task, we use thread concept.

        Ns thread class deal with function for creating thread semaphones monitors.

 NS user defaults:-

//saving user name and password into user defaults object:

   NSUserDefaults *defaults  =
[ nNSUserDefaults StandardUserDefaults];
[defaults SetObject: @ “Kumar” for key:@ “userName”];
[ defaults SetObject:@ “12345” for key: @ “password”];
[defaults synchronize];

getting user name and password from the user default

       NSUserDefault *defaultt2   =
    [ NSUserDefaults    StandardUserDefaults];
NSString *UserName =
[Default2 ObjectForkey  :@  “User Name”];
NSString * password  =
[Default2 ObjectForkey :@ “password”];
NSLog (@ “UserName is %@pass word is % @”, user name, password);

NSNumber:-

// Converting int to ns number:

Int x;
NSNumber      *a num = [NSNumber  NumberWithInt :x];
NSNumber        *b num=[NSNumber   NumberWithFloat:6.6];
NSNumber        *c num  =[ NSNumber   NumberWithChar: ‘c’];
NSNumber       *d num= [NSNumber   NumberWithBool :Yes];
NSArray          * a array=
     [NSArray  arrayWithObjects: a num, b num , c num, d num, nil];
NSLog  (@ “a array is % @”,  aArray);

//how to convert ns number to primary data types

 int   y=[ anum    intValue];
 float  y=[bnum     floatValue];
char ch  =[cnum  charvalue];
Bool   y=  [ dnum boolvalue ];


 

0 Responses on iPhone : Exploring the Foundation Framework – NS Thread & Number Class"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.