Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:15:16+00:00 2026-06-05T02:15:16+00:00

I’m finding it difficult to understand when it is necessary to create class methods.

  • 0

I’m finding it difficult to understand when it is necessary to create class methods. From what I’ve read, they are important for creating new objects, but I do not see how. The following class create a simple shape black rectangle. Can anyone show me how to incorporate a class method to do something that I could not do with an instance method?

Shape.h

#import <UIKit/UIKit.h>
@interface Shape : UIView; 
- (id) initWithX: (int)xVal andY: (int)yVal;
@end

Shape.m

#import "Shape.h"
@implementation Shape 
- (id) initWithX:(int )xVal andY:(int)yVal {
self = [super init];    
UIView *shape = [[UIView alloc] initWithFrame:CGRectMake(xVal, yVal, 10, 10)];
shape.backgroundColor = [UIColor blackColor];
[self addSubview:shape];
return self;
}
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:15:18+00:00Added an answer on June 5, 2026 at 2:15 am

    Class methods have several uses:

    • As you’ve heard, they’re essential for creating new objects. Creating objects is a two step process: first, you allocate memory for the object using the +alloc class method, then you initialize the object using some instance method that usually begins with -init. The +alloc method must be a class method because you don’t have an instance upon which to call it; you only know the class name. The -init method should be an instance method, because +alloc returns an instance, an you want to initialize the properties unique to that instance, not to the entire class. So, you used a class method then an instance method in your code above:
      UIView *shape = [[UIView alloc] initWithFrame:CGRectMake(xVal, yVal, 10, 10)];

    • Class methods are useful for retrieving singletons, or shared instances of a class. For example, the NSUserDefaults class stores preferences and settings associated with your app. In most cases, people like to use a single set of preferences for their entire app, so it has a class method +standardUserDefaults that returns a single instance of NSUserDefaultsthat everyone can use at any point in the app’s lifetime to get all the preferences associated with an app. Without that singleton and class method, you’d have to create and pass around an instance of NSUserDefaults throughout your app, which gets messy. Other examples include +[UIDevice currentDevice] and +[NSFileManager defaultManager].

    • Class methods are great for returning data or properties shared by all instances of the class. For example, UIView has a class method +layerClass that returns the kind of layer the view hosts. Since all instances of a particular UIView subclass use the same kind of layer, it has nothing to do with individual instances and thus makes sense as a class method.

    • You’ll see a lot of class methods called “convenience methods.” These methods (generally) combine calls to +alloc and -init and return an autoreleased instance of a class. For example, you can save some typing by writing [NSArray arrayWithObjects:...] instead of [[[NSArray alloc] initWithObjects:...] autorelease]. If you’re using ARC, these convenience methods are less useful, but they are still everywhere in Cocoa.

    • Class methods are also sometimes used for utility classes, or classes that don’t have state, and just have a bunch of methods that take in all the parameters they need. For example, you might have a method like +[PhoneUtilities parseAreaCodeFromPhoneNumber:string]. There are not many of these methods built-in to Cocoa; some people argue that it’s better to make them regular C functions, singletons, or regular objective-c classes with instance methods.

    I’m sure there are more, but that should give you some ideas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.