IPhone development tips

Source: Internet
Author: User

1:In Memory Management of the iPhone, when malloc is used to allocate memory on the heap, it is necessary to use the corresponding free release heap memory instead of simply release-related objects. The following code:

@ Interface memorytestprojviewcontroller: uiviewcontroller {

@ Public

Nsmutablearray * memoryarray;

}

 

@ End

 

@ Interface memorytestprojitem: nsobject

{

@ Public

Char * inneritem;

}

@ End

 

During initialization, The inneritem pointer allocates memory on the heap space, but remember to call the free function to release the heap space. The Code is as follows:

-(Void) viewdidunload {

// Release any retained subviews of the main view.

// E.g. Self. myoutlet = nil;

 

Memoryarray = [[nsmutablearray alloc] initwithcapacity: 10];

 

For (INT I = 0; I <1024; I ++)

{

Memorytestprojitem * Item = [[memorytestprojitem alloc] init];

Item-& gt; inneritem = (char *) malloc (1024 );

[Memoryarray addobject: item];

[Item release];

Free (item-> inneritem );

Free (item );

}

[Memoryarray removeallobjects];

}

II:If cfhttpmessageref is used for HTTP Communication Development in the iPhone, Content-Length content is not contained in the default header, and the display settings are required. The Code is as follows:

Nsstring * contentlen = [[nsstring alloc] initwithformat "% d", [bodydata length];

Cfhttpmessagesetheaderfieldvalue (myrequest,

Cfstr ("Content-Length"), (cfstringref) contentlen );

Cfdataref myserializedrequest = cfhttpmessagecopyserializedmessage (myrequest); // serialize the request

The above code is worth noting that cfstr is a macro in the iPhone and cannot dynamically set the Content-Length length for it. For example, the following code cannot be compiled:

Cfhttpmessagesetheaderfieldvalue (myrequest,

Cfstr ("Content-Length"), cfstr ([contentlen utf8string]);

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.