IOS procotol implementation - readonly property

I have a protocol from QuickLook:

/*!
 * @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 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 am trying to create a getter and setter for the readonly previewItemTitle property to add my custom snippet:

.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

Thus, as I understand it, I will only create a getter using the synhesize method. How to create a setter?

+5
source share
2 answers

If only in your implementation QLPreviewItemCustomyou want to access the installer, then why not extend the property in the read-write extension class category:

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, you will need to use a different ivar name and write your getter for the original to go 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

, , . .. QLPreviewItemCustom - - ABCPreviewItemCustom.

+5

, . self.previewItemURL, , previewItemURL, , - .

-,

@interface QLPreviewItemCustom : NSObject <QLPreviewItem> {
    NSURL * _previewItemURL;
    NSString *_previewItemTitle;
}


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


@end

:

@implementation QLPreviewItemCustom

@synthesize previewItemTitle = _previewItemTitle;
@synthesize previewItemURL = _previewItemURL;

@end

_Name self.Name

+1

All Articles