ERROR
Hi I have a category with base64 encoding, I need to call thing from my main class method. The following is my category
嗨,我有一个base64编码的类别,我需要从我的主类方法调用的东西。以下是我的类别
CODE
#import <Foundation/Foundation.h>
@interface NSString (addition)
- (NSString *) base64StringFromData:(NSData *)data length:(int)length;
@end
in .m file
在.m文件中
#import "NSString+addition.h"
static char base64EncodingTable[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
@implementation NSString (addition)
- (NSString *) base64StringFromData: (NSData *)data length: (int)length {
unsigned long ixtext, lentext;
long ctremaining;
unsigned char input[3], output[4];
short i, charsonline = 0, ctcopy;
const unsigned char *raw;
NSMutableString *result;
lentext = [data length];
if (lentext < 1)
return @"";
result = [NSMutableString stringWithCapacity: lentext];
raw = [data bytes];
ixtext = 0;
while (true) {
ctremaining = lentext - ixtext;
if (ctremaining <= 0)
break;
for (i = 0; i < 3; i++) {
unsigned long ix = ixtext + i;
if (ix < lentext)
input[i] = raw[ix];
else
input[i] = 0;
}
output[0] = (input[0] & 0xFC) >> 2;
output[1] = ((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4);
output[2] = ((input[1] & 0x0F) << 2) | ((input[2] & 0xC0) >> 6);
output[3] = input[2] & 0x3F;
ctcopy = 4;
switch (ctremaining) {
case 1:
ctcopy = 2;
break;
case 2:
ctcopy = 3;
break;
}
for (i = 0; i < ctcopy; i++)
[result appendString: [NSString stringWithFormat: @"%c", base64EncodingTable[output[i]]]];
for (i = ctcopy; i < 4; i++)
[result appendString: @"="];
ixtext += 3;
charsonline += 4;
if ((length > 0) && (charsonline >= length))
charsonline = 0;
}
return result;
}
@end
My UIViewcontroller methode CODE
我的UIViewcontroller方法代码
I need to call ategory method from this method
我需要从这个方法调用ategory方法
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"completeTransaction...");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Transaction completed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
NSLog(@"naveen%@",transaction.transactionReceipt);
//CALL CATEGORY METHOD HERE
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
4 个解决方案
#1
1
NSString *tsr=[NSString base64StringFromData:transaction.transactionReceipt length:[transaction.transactionReceipt length]];
NSLog(@"%@",tsr);
#2
4
First import the category into the viewcontroller you need like this:
首先将类别导入到您需要的viewcontroller中,如下所示:
#import "NSString+addition.h"
Calling it:
NSString *newString = [myString base64StringFromData:data length:length];
Just an example. Change it according to your situation...
只是一个例子。根据你的情况改变它......
Your code shld look like this:
你的代码看起来像这样:
NSString *tsr = [receiptStr base64StringFromData:transaction.transactionReceipt length:[transaction.transactionReceipt length]];
EDIT:
You need to add the category in the "NSString+addition.m" under the Compile Sources in Build Phases tab under Project Settings.
您需要在“项目设置”下的“构建阶段”选项卡中的“编译源”下的“NSString + addition.m”中添加类别。
#3
1
First you need to import your category in your UIViewController
implementation file
首先,您需要在UIViewController实现文件中导入您的类别
#import "NSString+addition.h"
In your method you create a new string object and call the method like all other NSString methods:
在您的方法中,您创建一个新的字符串对象并像所有其他NSString方法一样调用该方法:
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[...]
NSString *base64String = [stringToConvert base64StringFromData:dataObject length:length];
[...]
}
Please also note that there is are some coding-conventions regarding categories:
还请注意,有一些关于类别的编码约定:
-
The name of a category starts with a capital letter. In your case you should rename it to Additions
类别的名称以大写字母开头。在您的情况下,您应该将其重命名为Additions
-
The file name has the following format NameOfClassToAddTheCategoryTo+DescriptiveNameOfNewFunctionality. In your case it might be "NSString+Base64".
文件名具有以下格式:NameOfClassToAddTheCategoryTo + DescriptiveNameOfNewFunctionality。在你的情况下,它可能是“NSString + Base64”。
Edit: Due to the new information posted, please first read the Apple documentation regarding Categories.
编辑:由于发布了新信息,请首先阅读有关类别的Apple文档。
Then change your base64StringFromData:dataObject:length:
method to be a class method (What is the difference between class and instance methods?):
然后将base64StringFromData:dataObject:length:方法更改为类方法(类和实例方法之间有什么区别?):
+ (NSString *)base64StringFromData:(NSData *)data length:(int)length
{
[...]
return [result copy];
}
Finally you can call
最后你可以打电话
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[...]
NSData *receiptData = [transaction transactionReceipt];
int receiptDataLength = [transaction transactionReceipt] length];
NSString *base64String = [NSString base64StringFromData:receiptData length:receiptDataLength];
[...]
}
#4
0
change method:
- (NSString *) base64StringFromData: (NSData *)data length: (int)length;
to:
+ (NSString *) base64StringFromData: (NSData *)data length: (int)length;
Because the receiptStr
did not init.
因为receiptStr没有init。
#1
1
NSString *tsr=[NSString base64StringFromData:transaction.transactionReceipt length:[transaction.transactionReceipt length]];
NSLog(@"%@",tsr);
#2
4
First import the category into the viewcontroller you need like this:
首先将类别导入到您需要的viewcontroller中,如下所示:
#import "NSString+addition.h"
Calling it:
NSString *newString = [myString base64StringFromData:data length:length];
Just an example. Change it according to your situation...
只是一个例子。根据你的情况改变它......
Your code shld look like this:
你的代码看起来像这样:
NSString *tsr = [receiptStr base64StringFromData:transaction.transactionReceipt length:[transaction.transactionReceipt length]];
EDIT:
You need to add the category in the "NSString+addition.m" under the Compile Sources in Build Phases tab under Project Settings.
您需要在“项目设置”下的“构建阶段”选项卡中的“编译源”下的“NSString + addition.m”中添加类别。
#3
1
First you need to import your category in your UIViewController
implementation file
首先,您需要在UIViewController实现文件中导入您的类别
#import "NSString+addition.h"
In your method you create a new string object and call the method like all other NSString methods:
在您的方法中,您创建一个新的字符串对象并像所有其他NSString方法一样调用该方法:
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[...]
NSString *base64String = [stringToConvert base64StringFromData:dataObject length:length];
[...]
}
Please also note that there is are some coding-conventions regarding categories:
还请注意,有一些关于类别的编码约定:
-
The name of a category starts with a capital letter. In your case you should rename it to Additions
类别的名称以大写字母开头。在您的情况下,您应该将其重命名为Additions
-
The file name has the following format NameOfClassToAddTheCategoryTo+DescriptiveNameOfNewFunctionality. In your case it might be "NSString+Base64".
文件名具有以下格式:NameOfClassToAddTheCategoryTo + DescriptiveNameOfNewFunctionality。在你的情况下,它可能是“NSString + Base64”。
Edit: Due to the new information posted, please first read the Apple documentation regarding Categories.
编辑:由于发布了新信息,请首先阅读有关类别的Apple文档。
Then change your base64StringFromData:dataObject:length:
method to be a class method (What is the difference between class and instance methods?):
然后将base64StringFromData:dataObject:length:方法更改为类方法(类和实例方法之间有什么区别?):
+ (NSString *)base64StringFromData:(NSData *)data length:(int)length
{
[...]
return [result copy];
}
Finally you can call
最后你可以打电话
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[...]
NSData *receiptData = [transaction transactionReceipt];
int receiptDataLength = [transaction transactionReceipt] length];
NSString *base64String = [NSString base64StringFromData:receiptData length:receiptDataLength];
[...]
}
#4
0
change method:
- (NSString *) base64StringFromData: (NSData *)data length: (int)length;
to:
+ (NSString *) base64StringFromData: (NSData *)data length: (int)length;
Because the receiptStr
did not init.
因为receiptStr没有init。