ios custom NSObject casting


If I have a custom NSObject called Human which has a subclass called Male and I have an array called humans containing Human objects. When iterating over the humans array can I cast the object such as:

for (Human *human in humans) { Male *male = (Male *)human; }

or is it better to create a method to initWithMale such as

for (Human *human in humans) { Male *male = [[Male alloc] initWithMale:(Male *)human]; }

What would be the best approach from a memory management point of view or would it not matter? If it is the latter then how would I manage this in my initWithMale method?



It depends on what you are trying to accomplish. If the objects in the humans array are direct instances of Human, then you cannot cast them to any subclass of Human as they are not of that type. If this scenario is correct and you are trying to convert a Human into a Male, then you will need to create a init method in the Male class that can initiate a new object using a supplied Human:

Male *male = [[Male alloc] initWithHuman: human];

With this approach, your initWithHuman method would either need to retain the passed in Human instance and reference its values, or copy any necessary data. The later approach could be added to the Human class itself and that would allow you to initiate any subclass using the initWithHuman method (in essence, creating a basic copy function).

If the humans array contains subclasses of Human, then you can cast them to the correct instance, however, you should check to see if they are that instance first. Here is an example:

for (Human *human in humans) { if ([human isKindOfClass:[Male class]]) { Male *male = (Male *) human; } }


You don't need to cast an object of type id.


  • Javascript: For in loops iterating over properties or indices?
  • echo before deleting file through batch file
  • Array access optimization
  • Simple way to Delete the Last Child of a GameObject
  • C++ calling the default constructor with parens vs without parens [duplicate]
  • TCP Ping in Silverlight
  • How to obtain service credentials for a service instance created on IBM Bluemix without binding the
  • Hakyll says “Dependency cycle detected: …”
  • how to automatically enter password when using ssh?
  • why 0.1+0.2-0.3= 5.5511151231258E-17 in php [duplicate]
  • Yii2: Using Kartik Depdrop Widget?
  • How to add the custom button on google's(device) native application in android?
  • How do you keep a running instance for Google App Engine
  • Compare struct to a constant in C
  • Django Haystack Rebuild Index
  • HttpListener.IsSupported is false on XP SP3
  • Question about instantiating object
  • Spark job failing in YARN mode
  • NSScanner Loop Question
  • OOP Javascript - Is “get property” method necessary?
  • jQuery ready not fired after rails link_to is clicked
  • Create DicomImage from scratch using Dcmtk
  • How to do unit test for HttpContext.Current.Server.MapPath
  • Illegal mix of collations for operation for date/time comparison
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How would I use PHP exceptions to define a redirect?
  • Trying to switch camera back to front but getting exception
  • Eraser for UIBezierPath
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Compare two NSDates in iPhone
  • Comma separated Values
  • Benchmarking RAM performance - UWP and C#
  • Error creating VM instance in Google Compute Engine
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • How does Linux kernel interrupt the application?
  • Programmatically clearing map cache