2012-03-12 38 views
5

Tôi có một giao thức từ khuôn khổ QuickLook:Triển khai iOS procotol - tài sản readonly

/*! 
* @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 

Tôi đang cố gắng để tạo ra các phương thức getter và setter cho các tài sản previewItemTitle readonly vì vậy tôi có thể thêm gạch tùy chỉnh của tôi:

.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 

Bằng cách này, như tôi hiểu, tôi sẽ tạo ra chỉ getter với phương pháp tổng hợp. Làm thế nào tôi có thể tạo setter?

+2

Tại sao bạn cần setter cho tài sản chỉ đọc? – Antigluk

+0

Bởi vì đây là những gì tài liệu hướng dẫn của khung nhìn Quick Look nói, khi bạn cần đặt tiêu đề tùy chỉnh. – Benites

Trả lời

5

Nếu nó chỉ là trong việc thực hiện lại QLPreviewItemCustom mà bạn muốn truy cập vào setter, thì tại sao không mở rộng tài sản trong một thể loại lớp tiếp tục đọc-ghi:

QLPreviewItemCustom.m

#import "QLPreviewItemCustom.h" 

@interface QLPreviewItemCustom() 

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

@end 

@implementation QLPreviewItemCustom 

@synthesize previewItemTitle; 
@synthesize previewItemURL; 

@end 

Nếu bạn muốn sử dụng setter ở khắp mọi nơi thì bạn sẽ phải sử dụng một tên ivar khác và viết getter của bạn cho bản gốc để đi qua đến cái mới của bạn. Như thế này:

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 

Nó có giá trị cũng chỉ ra rằng nó không phải là nói chung là một ý tưởng tốt để sử dụng tiền tố cùng lớp mình là được sử dụng bởi một khuôn khổ khác. tức là đừng gọi nó là QLPreviewItemCustom - gọi nó là ABCPreviewItemCustom.

+0

Tôi cần truy cập nó từ bên ngoài lớp học. PreviewControllerClass.previewItemTitle trả về đối tượng QLPreviewItem. Hơn tôi làm cho QLPreviewItemCustom * title = PreviewControllerClass.previewItemTitle. Và sau đó tôi muốn thiết lập previewItemTitle, nhưng trước tiên tôi cần phải tạo ra setter đó một cách chính xác. – Benites

+0

Vậy tại sao bạn lại tuyên bố nó chỉ đọc? – mattjgalloway

+0

Xin lỗi, tôi có thể hỏi những câu hỏi sai vì tôi mới ở lập trình iOS. Thuộc tính previewItemTitle được định nghĩa chỉ đọc trong giao thức và nếu tôi khai báo nó theo cách khác, tôi sẽ nhận được cảnh báo. Vì vậy, về cơ bản câu hỏi của tôi là: Làm thế nào tôi có thể thực hiện một giao thức khung có một thuộc tính chỉ đọc theo cách mà tôi làm cho thuộc tính đó cũng có thể ghi? – Benites

1

Bạn có thể ghi trực tiếp vào biến số, không phải trong thuộc tính. Nếu bạn viết self.previewItemURL, bạn sẽ truy cập thuộc tính, nhưng nếu bạn chỉ viết previewItemURL bạn sẽ truy cập biến và bạn có thể ghi vào nó mà không có bất kỳ hạn chế nào.

Để hiển thị nhiều hơn bạn có thể đặt tên lĩnh vực của bạn khác nhau, ví dụ

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


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


@end 

và thực hiện:

@implementation QLPreviewItemCustom 

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

@end 

Bây giờ, bạn có thể truy cập vào lĩnh vực trực tiếp qua _Name và tài sản như self.Name

Các vấn đề liên quan