Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing iOS procotol - readonly property

Tags:

ios

I have a protocol from QuickLook framework:

/*!
 * @abstract The QLPreviewItem protocol declares the methods that a QLPreviewController  instance uses to access the contents of a given item.
 */
@protocol QLPreviewItem <NSObject>

@required

/*!
 * @abstract The URL of the item to preview.
 * @discussion The URL must be a file URL. 
 */
@property(readonly) NSURL * previewItemURL;

@optional

/*!
 * @abstract The item's title this will be used as apparent item title.
 * @discussion The title replaces the default item display name. This property is optional.
 */
@property(readonly) NSString * previewItemTitle;


@end

/*!
 * @abstract This category makes NSURL instances as suitable items for the Preview Controller.
 */
@interface NSURL (QLPreviewConvenienceAdditions) <QLPreviewItem>
@end

I'm trying to create the getter and setter for the readonly property previewItemTitle so I can add my custom tile:

.h

#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>

@interface QLPreviewItemCustom : NSObject <QLPreviewItem> {
    NSURL * previewItemURL;
    NSString *previewItemTitle;
}


@property(readonly) NSURL * previewItemURL;
@property (readonly) NSString *previewItemTitle;


@end

.m

#import "QLPreviewItemCustom.h"


@implementation QLPreviewItemCustom

@synthesize previewItemTitle;
@synthesize previewItemURL;


@end

This way, as I understand, I will create just the getter with the synthesize method. How can I create the setter?

like image 946
Marcelo Benites Avatar asked Mar 12 '12 20:03

Marcelo Benites


1 Answers

If it's just in your implementation of QLPreviewItemCustom that you want to access the setter, then why not extend the property in a class continuation category to read-write:

QLPreviewItemCustom.m

#import "QLPreviewItemCustom.h"

@interface QLPreviewItemCustom ()

@property (readwrite) NSURL *previewItemURL;
@property (readwrite) NSString *previewItemTitle;

@end

@implementation QLPreviewItemCustom

@synthesize previewItemTitle;
@synthesize previewItemURL;

@end

If you want to use the setter everywhere then you'll have to use a different ivar name and write your getter for the original to pass through to your new one. Like this:

QLPreviewItemCustom.h

#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>

@interface QLPreviewItemCustom : NSObject <QLPreviewItem> {
    NSURL *url;
    NSString *title;
}


@property (readwrite) NSURL *url;
@property (readwrite) NSString *title;

@end

QLPreviewItemCustom.m

#import "QLPreviewItemCustom.h"

@implementation QLPreviewItemCustom

@synthesize url;
@synthesize title;

- (NSURL*)previewItemURL {
    return self.url;
}

- (NSString*)previewItemTitle {
    return self.title;
}

@end

It's worth also pointing out that it's not generally a good idea to use the same class prefix yourself as is used by another framework. i.e. don't call it QLPreviewItemCustom - call it something like ABCPreviewItemCustom.

like image 178
mattjgalloway Avatar answered Sep 28 '22 11:09

mattjgalloway