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 the first time we defined iOS apps, I had to learn objective-c related grammar and knowledge for developers from Qt/c++.
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 many other brain dumps rather than a single tutorial
So I still hope it will be very helpful for you, I will first write the differences in programming languages and then write about the differences in the 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 are also obj-c++ file extension. mm, although I can't write it now.
Similar to Symbian C++,obj-c is constructed using two stages: first you allocate objects on the heap and then call the Init method.
It is usually possible to avoid calling two methods and simply using a static and simple method to give you a newly allocated object (such as stringwithcstring).
Completely different (at first) is the method of calling 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 a sample, this is an object that has a method that includes two parameters: obj (obj methodname:param1value param2:param2value]. It looks very strange and even awkward, but you have to get used to it.
In Objective C, which is commonly referred to as sending a message, it is not just the calling method, but also a lot more confusing I find.
In the example above, 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 just use the Qobject class, and obj-c here you use NSObject to do whatever it is.
Since iOS 5 implemented arc (own active reference count), feel 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 (add reference count) and release (decrement reference count and finally destroy) statements in your code.
I think it's great, assuming you're sticking to the normal way of doing things, you can basically stop worrying 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 to make a getter/setter for you.
Of course, you can also customize the Getter/setter to implement many other internal logic, such as implementing deferred initialization.
Can you imagine objective C entrusted as a set of slots. A entrusted method of the entrusted object is called by the 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 the AppendFormat is really very practical.


Assuming you want to replace, stringbyreplacingoccurrencesofstring is your friend and gives you a new NSString object. If you build the path to the file system, you can use stringByAppendingPathComponent.


Like in C + +, a base type like int is not an object. Suppose you need to wrap it 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 (such as Nsmutablearray).

Suppose 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 folder operations. You can very 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, to be able to see Nsmutabledata.

You can get the char pointer by data or by visiting the Mutabledata method.
I think most mobile applications today use HTTP. In QT you will use Qnetworkaccessmanager. In Objective C, you use a nsmutableurlrequest nsurlconnection inside (or nsurlrequest). You need to set nsurlconnection trust.
Entrusted to handle asynchronous events when generating a download (Readyread signal Connection: didreceivedata: Complete signal connectiondidfinishloading: etc.).


Assuming you want to do socket-based IO, I can only recommend Gcdasyncsocket. I have tried the socket programming Iquassel project for several reasons, mainly because you have to use carbon instead of cocoa. Gcdasyncsocket also has the advantage of having a very easy way to resolve network protocols in one thread to avoid clogging the UI thread.
Talking about threading: You can achieve major concurrency by using NSObject's performselectorinbackground. It enables a method to execute in a background thread. It can communicate its results to the main thread via Performselectoronmainthread.

Suppose you want to make a qt-tish 0 timer call, check the Afterdelay:performselector mutation, and the selector executes in an event loop call.


Many other advanced advanced things can be scheduled with nsoperationqueue or the central CPU.
Nsrunloop is equivalent to Qeventloop in Qt.

Suppose you have a thread that handles things and requires an event loop, such as network IO.

Then you can use it to handle it.
Nsuserdefaults is a very 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 an iOS device, so you can use it.

For simple sax-style parsing, at least Nsxmlparser can deal with it.


3.UI Things
Xcode has visual designers (including interface builders), and designers can design a storyboard (Uistoryboard) that includes 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 every view controller when a given event occurs by an OS.
You use the "outlets" link with their same instance of the control in the Interface Builder UI file (such as UIButton, UILabel) and drag with the CTRL key combination.
QT's many views and controls, I do not do too much with the corresponding obj-c aspect of the control of the comparison. Here's a look at the contents of UITableView including using your Uitableviewdatasource and uitableviewdelegate.

I want to say almost all iOS apps use the table view.
Let's say you've used Qwebview, and UIWebView is the same.
Qimage and Qpixmap equate to cgdataproviderref,nsimage and cgimage.
You can use the core graphics method to define your own drawing. Uigraphicsbeginimagecontextwithoptions creates a context in which you can use the paint feature. Suppose 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 classes correspond to the OS X and UI * classes corresponding to iOS.
4. Misc
When I developed QT, I used Qtcreator, my co-founder Oliver was a fan of KDevelop, and he should also be very fond of qtcreator, for iOS development, you can use Apple's free Xcode development tool.


Apple's Project division also provides you with some unit tests similar to QT qtest. In Xcode, create a new test class and usage such as: Stassertequals stasserttrue and so on.
5.Want to know more?


I suspect that the QT Developer Forum site (http://qt-project.org/forums/) is equivalent to Stackoverflow,ios programming most of the problems in StackOverflow are able to find answers and help. Another in-house Apple Developer Forum (https://idmsa.apple.com/IDMSWebAuth/login?appIdKey= 4A75046CDA87EAB6386A9EAE8CAABB9824E328B9ABC988119B39296495EC184C&PATH=/LOGIN.JSPA).


Of course, suppose you have QT applied to iOS regardless of need help. This is WOBOQ. You can do one thing! 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.