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”); }
** 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.
>> 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.
//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);
// 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
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
Name | Dates | |
---|---|---|
IOS Development Training | Feb 04 to Feb 19 | |
IOS Development Training | Feb 07 to Feb 22 | |
IOS Development Training | Feb 11 to Feb 26 | |
IOS Development Training | Feb 14 to Mar 01 |
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.
1 /15
Copyright © 2013 - 2023 MindMajix Technologies