Home /

Exploring the Foundation Framework in iPhone - NS Thread & Number Class

Rating: 4.0Blog-star
Views: 1975
by Ravindra Savaram
Last modified: February 15th 2021

NS Thread & Number Class

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”);
}

Multithreading:-

** 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 the added speed in objective C, there is a special class for threading i.e. NSThread class.

Inclined to build a profession as IOS Developer? Then here is the blog post on iPhone Development Certification Training.

Ns thread:-

>> If you need to run the relative task on the background of the application after the same task, we use the thread concept.
>> Ns thread class deals with function for creating thread semaphores 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 ];

 

Frequently Asked iPhone Interview Questions & Answers

 

About Author

author
NameRavindra Savaram
Author Bio

 

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.