Extending the class for basic data methods

Can anyone please skip me on how to "hide" the default kernel data boxes?

I know there is actually no way to define 'Private' methods in Objective-C, but read about using an extension to achieve a similar result. The problem is I want to apply this to the main data classes. I would like to hide the standard setters created for some attributes and only call them from other public exponents.

Example: my main data object has a BOOL 'collected' and a date 'DateCollected'. I figured out how to add setDateCollected to setCollected, but now I would like to hide the set so that it cannot be called directly so easily (when I might have forgotten to set dateCollected manually as well).

To clarify, the part that gets me disconnected is the @dynamic calls - I don't know where they should live.

EDIT - I guess I missed the part. I can move the @property declaration to the implementation file just fine. But I want the setter to be hidden and the getter to stay open. I think I need to replace @property, but I don't know how to do that for the main data object.

+3


source to share


2 answers


You define private methods in the implementation file. If a method is not shown in the header file, then by definition it is private. To clarify, the following is how you define public properties and methods.

@interface MyAppViewController : UIViewController

@property (strong, nonatomic) NSString *myPublicStringObject;

- (NSString *) myPublicMethodTakingInputString:(NSString *) input;

@end

      

In the implementation file, you can define private properties and methods as follows.



#import "MyAppViewController.h"

// Declare private properties and methods inside the following interface extension
@interface MyAppViewController ()

@property (strong, nonatomic) NSNumber     *myPrivateNumber;
@property (strong, nonatomic) NSString     *myPrivateStringObject;

- (void) myPrivateMethod1;
- (NSString *) myPrivateMethod2WithIntegerInput:(NSInteger) input;

@end


@implementation MyAppViewController

@synthesize myPublicStringObject;
@synthesize myPrivateNumber;
@synthesize myPrivateStringObject;

// Implement all methods declared in header and private interfaces here in no particular order
- (NSString *) myPublicMethodTakingInputString:(NSString *) input
{
// code for myPublicMethodTakingInputString
}

- (void) myPrivateMethod1
{
}

- (NSString *) myPrivateMethod2WithIntegerInput:(NSInteger) input
{
}


// Implement any other private method not declared
- (NSString *) myUndeclaredPrivateMethod1:(NSString *) input
{
// code for myUndeclaredPrivateMethod1
}


- (id) myUndeclaredPrivateMethod2
{
// code for myUndeclaredPrivateMethod2
}

      

... etc.

This description is for iOS 5, of course. The syntax for iOS3 and 4 is similar, except that the private variables (iVars) appear in the header file, which can be very confusing for newbies. iOS 5 cleared this up by not requiring instant variables to appear.

0


source


What you are trying to do is unlikely to lead to a good result. Core Data classes are very delicately configured with a graph hierarchy and property implementations in a superclass. Maybe you should pull out the collected variable and just do a null check on the Collected date. An even slightly more confusing way would be to remove it from the data model and make it a regular ivar.



EDIT: Ok, I looked at the information in your comment. This really means that you can override the implementation if you follow certain guidelines. However, the answer below is probably better. Move the property to a private interface. Then declare another property on a public (read-only) interface that returns the value of the private property;).

+1


source







All Articles