Documented or undocumented, a feature, sometimes, you are forced to implement by force! Useful in some situations, the scroll is to remove an object from UIWebView . : Interestingly, in general, see how you can access the components 'internal' part of the object in question: in this example retrieves the object pointer UIScrollView present in the subject UIWebView :
Category 'D'
Very short snippet: Remove scroll from one object to UIWebView
Very short snippet: NSURL, and various possible
Returns the name of a file from the object NSURLRequest , allowing you to decide whether or not its extension:
Very short snippet: get the number of posts, Page and Attachment
How many of you know, the table wp_posts is used to store the WordPress post, pages and attachments. The type of the item stored is determined by the field post_type . . With select proposed below, you can "count" individually the various types of elements in memrizzate wp_posts .
Very short snippet: recording an image in the Photo Album
Easy way to save an image in the photo album of the device inside of our application (or loaded from remote):
NSURLConnection: Example of use
NSURLConnection allows for controlled from a connection to a remote server. To use it simply:
1 2 3 4 5 6 7 8 9 10 11 | / / The objects sufficient to perform a NSURLConnection urlConnection; NSURLConnection * URLConnection; mutableData; NSMutableData mutableData *; / / ... urlString = @ "http://www.miodominio.com/documento.txt" ; NSString * urlString @ = "http://www.miodominio.com/documento.txt"; urlRequest = [ NSURLRequest requestWithURL : [ NSURL URLWithString : urlString ] ] ; NSURLRequest URLRequest = * [ NSURLRequest requestWithURL: [ NSURL URLWithString: urlString]]; / / The delegate will respond to the states of the connection NSURLConnection alloc ] initWithRequest : urlRequest delegate : self ] ; URLConnection = [[ NSURLConnection alloc] initWithRequest: URLRequest delegate: self]; |
Here are delegate methods to check the status of the connection:
[Cc_objc]
Very short snippet: Objective-C NSString from Selector
Objective-C is a wonderful language that allows you to do amazing things. One of the most interesting aspects is its dynamic invocation of methods (messages). You can, in fact, get the address of a message from a string.
Very short snippet: UIWebView, and display PDF files inside
The object UIWebView can be used to display several files. For example you can use it to display - as well as QuickTime movies or YouTube - PDF or HTML file included in our code.
NSString
NSString is a powerful class, let me show you some of the most used properties:
printf ()
1 2 |
Run the split ()
1 2 3 |
Convert from string to value
1 2 3 | / / Converting doubleString = @ "123" ; NSString * @ doubleString = "123"; [ doubleString doubleValue ] ; double value = [doubleString doubleValue]; |
Within a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | / / Substring searchString = @ "age" ; NSString * @ searchString = 'age'; beginsTest = @ "Agencies" ; NSString * beginsTest @ = "Agencies"; [ beginsTest rangeOfString : searchString NSRange prefixRange = [beginsTest rangeOfString: searchString NSAnchoredSearch | NSCaseInsensitiveSearch ) ] ; options: (NSAnchoredSearch | NSCaseInsensitiveSearch)]; / / PrefixRange = {0, 3} endsTest = @ "BRICOLAGE" ; NSString * endsTest = @ "DIY"; [ endsTest rangeOfString : searchString NSRange suffixRange = [endsTest rangeOfString: searchString NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwardsSearch ) ] ; options: (NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwardsSearch)]; / / SuffixRange = {6, 3} |
Very short snippet: composing mail in an application iPhone, iPod or iPad
To compose mail in an application iPhone / iPod, simply add the framework MessageUI . In our controller to enter the inclusion of the framework and adopt the Protocolo MFMailComposeViewControllerDelegate :
Objective-C class methods and self alloc
davanti al prototipo, tipo: When you define and use methods (messages) under Objective-C, we are often faced with the curious syntax that shows a sign - or + in front of the prototype, type:
1 2 3 4 5 6 7 | / / In the definition void ) mioMessaggio; - (Void) mioMessaggio; / / Similarly, in the implementation void ) mioMessaggio { - (Void) {mioMessaggio / / ... } |
Or:
1 2 3 4 5 6 7 | / / In the definition void ) mioMessaggio; + (Void) mioMessaggio; / / Similarly, in the implementation void ) mioMessaggio { + (Void) {mioMessaggio / / ... } |
The difference is that the methods defined by the symbol - are instance methods, then linked to an object. The methods defined by the symbol + are called class methods, as they can be performed without allocating and instantiate the object in question.
sono due classi, molto usate, che contengono svariati metodi di classe. NSString or UIView are two classes, widely used, which contain several class methods. Class methods are used continuously, as when we initialize or allocate any object:
1 | [ UIView alloc ] ; MyView UIView * = [UIView alloc]; |
The method alloc is a classic example, present in all objects and, as is evident from the code, a class method is invoked as before allocation of the object itself.
Class methods can be useful in many cases, particularly when we create our object and we want to allocate and initialize it in less lines of code as possible. Imagine having to collect a number in an array of objects defined by us. We define our object, first of all, writing the code in the simplest way, without using class methods:
1 2 3 4 5 6 7 8 9 10 | / / Define the interface myObject.h # Import <Foundation/Foundation.h> NSObject { @ Interface myObject: NSObject { name; NSString * name; lastname; NSString * lastname; } nonatomic, retain ) NSString * name; @ Property (nonatomic, retain) NSString * name; nonatomic, retain ) NSString * lastname; @ Property (nonatomic, retain) NSString * lastname; |
The implementation, in the simplest case, nothing could be either:
1 2 3 4 5 6 7 8 9 10 11 12 | / / MyObject.m # Import "myObject.h" @ Implementation myObject @ Synthesize name, lastname; void ) dealloc { - (Void) dealloc { ; [Name release]; ; [Lastname release]; ; [Super dealloc]; } |
When we get to use our object, we will use a code like this:
1 2 3 | [ myObject alloc ] ; myObject * obj = [myObject alloc]; "Mario" ; obj.name @ = "John"; "Rossi" ; obj.lastname @ = "Smith"; |
If we wanted to create many objects of this type, and place them in an NSArray , the situation becomes a little awkward:
1 2 3 4 5 6 7 8 9 10 11 12 | [ myObject alloc ] ; objA * myObject = [myObject alloc]; "Mario" ; objA.name @ = "John"; "Rossi" ; objA.lastname @ = "Smith"; [ myObject alloc ] ; objB * myObject = [myObject alloc]; "Carlo" ; objB.name @ = "Charles"; "Bianchi" ; objB.lastname @ = "Smith"; elenco = [ NSArray arrayWithObjects : objA, objB, nil ] ; NSArray * list = [ NSArray arrayWithObjects: objA, objB, nil]; ; [ObjA release]; ; [ObjB release]; |
per aggiungere man mano gli oggetti nel nostro elenco. You could improve the code by creating a loop for or using an NSMutableArray to add as objects in our directory. . However, the situation of migliorebbe little, when they would remain outside the property settings name and lastname . Would then be spontaneous, to start, add a method - object - initWithName that would allow you to jump to the property settings, simplifying things a bit. In the implementation file myObject.m add:
1 2 3 4 5 6 7 | id ) initWithName : ( NSString * ) stringName lastname : ( NSString * ) stringLastname { - (Id) initWithName: ( NSString *) StringName lastname: ( NSString *) {stringLastname self = [ super init ] ) { if (self = [super init]) { self.name = StringName; self.lastname = stringLastname; } return self; } |
In doing so we have improved the situation, they can now write:
1 2 3 4 5 6 7 | [ [ myObject alloc ] initWithName : @ "Mario" lastname : @ "Rossi" ] ; objA * myObject = [[myObject alloc] initWithName: @ "John" lastname: @ "Smith"]; [ [ myObject alloc ] initWithName : @ "Carlo" lastname : @ "Bianchi" ] ; objB * myObject = [[myObject alloc] initWithName: @ "Charles" lastname: @ "Smith"]; elenco = [ NSArray arrayWithObjects : objA, objB, nil ] ; NSArray * list = [ NSArray arrayWithObjects: objA, objB, nil]; ; [ObjA release]; ; [ObjB release]; |
, necessari per l'inserimento nell'array e liberare la memoria. Yet still have pointers objA and objB , necessary for the entry in the array and free memory. Wishing we could be entered directly in the creation of an object populating the array, using autorelease to free up memory, but the code would still not idle. Let me demonstrate how to solve the issue with a class method. First of all we replace - (id)initWidthName with:
1 2 3 4 5 6 7 8 9 10 | id ) initWithName : ( NSString * ) name lastname : ( NSString * ) lastname { + (Id) initWithName: ( NSString *) name lastname: ( NSString *) {lastname myObject * item; item = [ [ self alloc ] init ] ) { if (item = [[self alloc] init]) { / / Init item.name = name; item.lastname = lastname; } item autorelease ] ; return [item autorelease]; } |
In doing so we created a class method that allocates (in autorelase) and actually initiated one of our object, first you have the pointer to the instance. The code used is then:
1 2 3 4 |
Much, much better ...






Latest Comments
Mark : @ Mark: Thanks for the answer. For example, if you have a code to save / use the folder ...
Mark : Thanks for the answer. For example, if you have a code to save / use the folder ...
Giovambattista Fazioli : @ Mark: in principle, absolutely, a lot depends on how and what to download. La. ..
Mark : Hello, good guide! I wanted to ask, you can use the same code to run a sql database? Instead of ...
Joseph : Hi I wanted to ask you how you can do in the post I uploaded a gallery of 50 images, for example, ...