gpt4 book ai didi

ios - 如何在另一个类中调用类别方法

转载 作者:行者123 更新时间:2023-11-28 18:32:53 25 4
gpt4 key购买 nike

我有类别类有方法来编码 url。那么如何在另一个类中使用这个方法。提前致谢

NSString+EncodeURL.h

#import <Foundation/Foundation.h>

@interface NSString (EncodeURL)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
@end

NSString+EncodeURL.m

-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", CFStringConvertNSStringEncodingToEncoding(encoding)));
}

在另一个类(class)。如何在类别类中使用 urlEncodeUsingEncoding 将 urlString 转换为字符串

#import "WatchVideosViewController.h"
#import "CustomCell.h"
#import "ImageRequest.h"
#import "Constant.h"
#import "ImageCache.h"
#import "NSString+EncodeURL.h"
@interface WatchVideosViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageBackground;

@end

@implementation WatchVideosViewController

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIndentifier = kCellName;
CustomCell *cell = (CustomCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifier forIndexPath:indexPath];
NSDictionary *dictVideo = [self.videoList objectAtIndex:indexPath.row];
//start indicator
[cell.indicator startAnimating];
//set title
NSString *titleVideo = [dictVideo objectForKey:kTitleKey];
[cell.myLabel setText:titleVideo];
// set image url
NSString *urlVideo = [dictVideo objectForKey:kUrlKey];
NSURL *url = [NSURL URLWithString:urlVideo];
NSString *urlString = [url absoluteString];
NSString *encodeURL=[urlString ]
//encode url

最佳答案

在你想使用-urlEncodeUsingEncoding:的文件中,只需添加:

#import "NSString+EncodeURL.h"

该文件中的所有 NSString 实例都将响应 -urlEncodeUsingEncoding:

关于ios - 如何在另一个类中调用类别方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24623354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com