Objective C (iOS) for QT C + + developers (iOS developer, what do Qt developers need to know?) )

Source: Internet
Author: User

Obj-c in the eyes of qt/c++ developers
For our first custom iOS app, I had to learn objective-c related grammar and knowledge for qt/c++ developers.
To make it easier for readers to understand the content of this blog, I'll describe a lot of what I've learned. This will be more of a brain dump than a single tutorial
So I still hope it will be very helpful for you, I will write the difference of programming language first, then write about the difference of class library
1.Objective c vs C vs C + +
Similar to C++,obj-c is a superset of the C language (which is not 100% correct, but a good enough statement to understand it), you will use the file name extension of the header. H and. M to represent the OBJ-C syntax
Note There is also a obj-c++ file extension. mm, although I can't write it yet.
Similar to Symbian C++,obj-c is constructed using two stages: first you allocate objects on the heap and then call the Init method.
You can usually avoid calling two methods and simply using a static and simple method to give you a newly allocated object (for example, stringwithcstring).
Completely different (very scattered at first) is the calling method of the Obj-c function, similar to the usual c-ish way, such as NSLog (@ "My log Messages"), but there are also obj-c syntax objects called methods.
As an example, this is an object that has a method that contains two parameters: obj (obj methodname:param1value param2:param2value]. It looks strange and even awkward, but you have to get used to it.
In Objective C, this is usually called sending a message, not just its calling method, but also a lot more confusing I find.
In the above example of this method, MethodName is the so-called selector. A selector is an identifier for a method. Sometimes you have to identify the method (similar to a function pointer),
In the example above you can write @selector (methodname:param2:).
There is no root object in C + + and QT Qobject is used only for some objects, Objective C has a mandatory root object nsobject.
Instead of QT you want signals/slots, you only use the Qobject class, and obj-c here you use NSObject to do anything.
Since iOS 5 implemented arc (auto reference count), think of it like an implicit qsharedpointer in your object. The encoding process feels like you have a garbage collector. Internally, Arc tells the compiler to insert a hold (increase reference count) and release (decrement reference count and final destroy) statements in your code.
I think it's great that if you stick to the normal way of doing things you can basically no longer worry about memory leaks.
The Objective C property is similar to the Q_property in Qt, which means that you can use the good Obj.var = foo syntax inside the code (obj setvar:foo) message called (OK: Message sent). You can create an attribute @property and the compiler uses @synthesize for you to make a getter/setter.
Of course, you can also customize Getter/setter to implement more internal logic, such as implementing lazy initialization.
Can you imagine objective C delegated as a set of slots. A delegate method of a delegate object is called by an object to notify that something has happened. This is very similar to the Java interface
2. Something similar to a QT-related class
As important as syntax is the associated library provided by iOS (and Os X). Read on to learn about the objects and features they provide.
The NSString object is a constant string. You can also create a pass @ "followed by me". There is a mutable string that must be used with nsmutablestring to match its appendstring,appendformat functions. Especially AppendFormat is really useful.
If you want to make a replacement, stringbyreplacingoccurrencesofstring is your friend and gives you a new NSString object. If you are building a file system path, you can use stringByAppendingPathComponent.
Like in C + +, a base type like int is not an object. If you need to wrap in an object, you can use NSNumber and Nsvalue, Qvariant (similar). As far as I know: You can use the Intvalue NSNumber or NSString method to convert to an int type.
Nsarray, Nsset nsdictionary They sound like: a container to store nsobjects. Nsarray equivalent to Qlist,nsset equals Qset, and nsdictionary is similar to Qhash/qmap.
You need to change them using their mutable generics (for example, Nsmutablearray). If you want to store the original type, you need to use NSNumber, Nsvalue and other packaging. For Nsarray, you can access objects by using Objectatindex.
Nsdictionary, you use Valueforkey or Objectforkey.
I didn't do much about the file IO aspect, so I couldn't write too much. There are Nsfilemanager directory operations. You can easily read (small) files through NSString stringwithcontentsoffile or NSData datawithcontentsoffile.
Remember that both OS X and iOS systems are based on POSIX standards, so you can also use a system approach to get raw performance or more efficient.
Speaking of NSData: this is equivalent to QT in Qbytearray, mutable generics that can look at nsmutabledata. You can get the char * pointer by data or by accessing the Mutabledata method.
I think most mobile applications now use HTTP. In QT you will use Qnetworkaccessmanager. In Objective C, you use a nsmutableurlrequest nsurlconnection inside (or nsurlrequest). You need to set the Nsurlconnection delegate.
Delegate handles asynchronous events when generating a download (Readyread signal Connection: didreceivedata: Complete signal connectiondidfinishloading: etc.).
If you want to do socket-based IO, I can only recommend Gcdasyncsocket. I've tried socket programming Iquassel projects for several reasons, mainly because you have to use carbon instead of cocoa. Gcdasyncsocket also has a good advantage, and you can easily parse the network protocol in one thread to avoid blocking the UI thread.
Talking about threading: You can achieve basic concurrency by using NSObject's performselectorinbackground. It enables a method to run in a background thread. It can communicate its results to the main thread via Performselectoronmainthread. If you want to make a qt-tish 0 timer call, check the Afterdelay:performselector mutation, the selector runs in an event loop call.
More advanced things can be scheduled with nsoperationqueue or central CPU.
Nsrunloop is equivalent to Qeventloop in Qt. If you are dealing with things in another thread and need an event loop, such as network IO. Then you can use it to handle it.
Nsuserdefaults is a good way to store and load application settings (similar to qsettings).
Unfortunately, Obj-c does not have a related object similar to Qtxmlpatterns. LIBXML2 exists on iOS devices, so you can use the. For simple sax-style parsing, at least nsxmlparser can be dealt with.
3.UI Things
Xcode has visual designers (including interface builders), and designers can design a storyboard (Uistoryboard) that contains a large number of view controllers (Uiviewcontroller subclasses). Each view controller manages the view (Do you remember MVC?). Navigation between views occurs through evolution (Uistoryboardsegue).
You can move data between view controllers through the Prepareforsegue method of the Source view controller. Methods such as viewwillappear are called each view controller when it gets to occur by OS-specific events.
You use the "outlets" link with their same instance of the control in the Interface Builder UI file (for example, UIButton, UILabel) and drag with the CTRL key combination.
QT's many views and controls, I do not do much with the corresponding obj-c aspect of the control comparison. Here's a look at the contents of UITableView including using your Uitableviewdatasource and uitableviewdelegate. I want to say that almost all iOS applications use the table view.
If you have used Qwebview, change to UIWebView is the same.
Qimage and Qpixmap equate to cgdataproviderref,nsimage and cgimage.
You can customize the drawing using the method of the core graphic. Uigraphicsbeginimagecontextwithoptions creates a context in which you can use the drawing feature. If you want to get the context of the bitmap, try Uigraphicsgetimagefromcurrentimagecontext.
Qrect,qpoint is cgrect equivalent, Cgpoint cgsize. Helps you debug, check Nsstringfromcgrect and its friends.
For widgets, note that the NS * UI class corresponds to the OS X and UI * classes that correspond to iOS.
4. Misc
When I develop QT, I use Qtcreator, my co-founder Oliver is a fan of kdevelop, and he should also like Qtcreator, for iOS development, you can use Apple's free Xcode development tool.
Apple's engineers also provide you with some unit tests similar to QT qtest. In Xcode, create a new test class and use methods such as: Stassertequals stasserttrue and so on.
5.Want to know more?
I guess the QT Developer Forum website (http://qt-project.org/forums/) is the equivalent of Stackoverflow,ios programming most problems in StackOverflow can find answers and help. There is also an in-house Apple Developer Forum (https://idmsa.apple.com/IDMSWebAuth/login?appIdKey= 4A75046CDA87EAB6386A9EAE8CAABB9824E328B9ABC988119B39296495EC184C&PATH=/LOGIN.JSPA).
Of course, if you need any help porting the QT app to iOS. That's one thing Woboq can do for you! Please write to us.

Objective C (iOS) for QT C + + developers (iOS developer, what do Qt developers need to know?) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.