标签: nsdata

UIDocumentInteractionController"invalid scheme(null)"

我正在尝试使用UIDocumentInteractionController预览文档.该文档是我的应用程序从网上下载的.xls文件.我一直收到这个错误:

'UIDocumentInteractionController: invalid scheme (null).  Only the file scheme is supported.'
Run Code Online (Sandbox Code Playgroud)

我用下一个代码:

- (void) webServiceController:(WebServiceController *)webServiceController returnedArray:(NSMutableArray *)array {
    if ([webServiceController.webRequest isEqualToString: @"generateExcelFileForEmployee"]) {
        // start previewing the document at the current section index
        NSString *link = [[NSString stringWithString:array[0]] stringByReplacingOccurrencesOfString:@"AdminFunctions.php" withString:@"AdminFunctions.xls"];
        link = [[[link stringByReplacingOccurrencesOfString:@"\\" withString:@""]stringByReplacingOccurrencesOfString:@"/public/sites/" withString:@"http://"]stringByReplacingOccurrencesOfString:@"\"\\\"" withString:@""];
        NSLog(@"%@", link);
        NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:link]];

        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"AdminFunctions.xls"];

        BOOL isWriteSuccess = [urlData writeToFile:filePath atomically:YES];
        NSLog(@"%@", …
Run Code Online (Sandbox Code Playgroud)

excel preview nsdata ios uidocumentinteraction

22
推荐指数
1
解决办法
7865
查看次数

不使用UIImagePng表示或UIImageJpegRepresentation将UIImage转换为NSData

我需要转换UIImageNSData但不使用UIImagePngRepresentation或者UIImageJpegRepresentation,对于来自photolib的图像我可以使用此处提到的assetlib方法使用ALAssetsLibrary和ALAsset取出Image作为NSData,但对于捕获的图像,assset url不存在因此在那种情况下我需要转换UIImage直接用exif数据的字节,我该怎么做到这一点?请帮忙

assets uiimage nsdata ios uiimagepngrepresentation

21
推荐指数
2
解决办法
2万
查看次数

NSData不接受有效的base64编码字符串

我正在iOS(7)客户端实现JSON Web Token身份验证.它工作得很好.我的应用程序接收令牌,并可以使用它们对我的服务器进行经过身份验证的调用.

现在,我希望我的客户端代码检查令牌上的到期日期,以便它可以知道何时重新进行身份验证.检查JWT身份验证令牌的到期日期非常简单.授权令牌是3个base64编码的JSON blob,用'.'分隔.- 到期时间戳位于中间blob中,在一个名为的字段中ext.这是unix时代以来的几秒钟.

所以我的代码看起来像这样:

- (NSDate*) expirationDate
{
    if ( !_tokenAppearsValid ) return nil;

    if ( !_parsedExpirationDate )
    {
        //
        //  Token is three base64 encoded payloads separated by '.'
        //  The payload we want is the middle one, which is a JSON dict, with
        //  'exp' being the unix seconds timestamp of the expiration date
        //  Returning nil is appropriate if no 'exp' is findable
        //

        NSArray *components = [self.token componentsSeparatedByString:@"."];

        NSString *payload = …
Run Code Online (Sandbox Code Playgroud)

base64 json nsdata ios jwt

21
推荐指数
2
解决办法
1万
查看次数

NSData到UIImage

我正在尝试保存UIIMage,然后检索并显示它.我已成功将图像保存在捆绑中,从那里检索并显示.我的问题来自于我尝试将图像保存到磁盘(将其转换为NSData),然后检索它.我像这样将图像转换为NSData ...

NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);
Run Code Online (Sandbox Code Playgroud)

然后我把它写成磁盘就像这样......

[topImageData writeToFile:topPathToFile atomically:NO];
Run Code Online (Sandbox Code Playgroud)

然后我试着像这样检索它......

topSubView.image = [[UIImage alloc] initWithContentsOfFile:topPathToFile];
Run Code Online (Sandbox Code Playgroud)

它不返回任何图像(大小为零).所以我试过......

NSData *data = [[NSData alloc] initWithContentsOfFile:topPathToFile];
topSubView.image = [[UIImage alloc] initWithData:data];
Run Code Online (Sandbox Code Playgroud)

无济于事.当我单步执行调试器时,我确实看到数据包含正确的字节数,但我很困惑为什么我的图像没有被创建.我错过了一步吗?在转换为图像之前,我是否需要对NSData执行某些操作?

iphone uiimage nsdata

20
推荐指数
3
解决办法
6万
查看次数

将NSData转换为int

我有一个NSData对象,长度是4个字节.这四个字节我从另一个NSData对象中提取,使用,

fourByteData=[completeData subdataWithRange:NSMakeRange(0, 16)];
Run Code Online (Sandbox Code Playgroud)

我的第一个问题是,上面的语句是否会为我提供完整数据的前四个字节.

如果是,那么如何将所有这些字节转换为等效的int.

iphone objective-c nsdata ipad

20
推荐指数
4
解决办法
3万
查看次数

如何在Swift中将Int转换为NSData?

在Objective-C中,我使用以下代码

  1. Int变量转换NSData为字节数据包.

    int myScore = 0;
    NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将转换后的NSData变量用于方法.

    [match sendDataToAllPlayers: 
    packet withDataMode: GKMatchSendDataUnreliable 
    error: &error];
    
    Run Code Online (Sandbox Code Playgroud)

我尝试将Objective-C代码转换为Swift:

var myScore : Int = 0

func sendDataToAllPlayers(packet: Int!,
            withDataMode mode: GKMatchSendDataMode,
            error: NSErrorPointer) -> Bool {

            return true
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法将Int变量转换为a NSData并将其用作方法.我怎样才能做到这一点?

int nsdata ios swift

20
推荐指数
3
解决办法
2万
查看次数

iPhone - int到NSData?

我正在制作一个iPhone应用程序,我希望保存应用程序的状态.这包括一个int属性,我将自己持久保存到文件中.我有它编写和工作,但我知道我做的方式是有点hacky,将int转换为String然后转换为NSData.任何人都可以提出更好的方法吗?

int someInt = 1;
NSString *aString = [NSString stringWithFormat:@"%d",someInt];
NSData *someData = [aString dataUsingEncoding:NSUTF8StringEncoding];
[someData writeToFile:[documentsDirectory stringByAppendingString:@"someFile"] atomically:YES];
Run Code Online (Sandbox Code Playgroud)

然后从磁盘读取它并将其放回int -

NSData* someData = [NSData dataWithContentsOfFile:[documentsDirectory stringByAppendingString:@"someFile"]];
NSString *aString = [[NSString alloc] initWithData:someData encoding:NSUTF8StringEncoding];
int someInt = [aString intValue];
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsdata

19
推荐指数
1
解决办法
2万
查看次数

是否可以将NSMutableArray或NSDictionary数据保存为iOS中的文件?

我想保存NSMutableArrayNSDictionary作为永久文件.然后,我想稍后重新打开该文件.

这可能吗?如果是这样,我该怎么办?

cocoa-touch nsdictionary nsmutablearray nsdata ios

19
推荐指数
1
解决办法
2万
查看次数

将NSData附加到Objective C中的文件

我正在尝试在iPhone上下载一个大文件,并在下载时将其保存到磁盘.

理想情况下,在:

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {

}
Run Code Online (Sandbox Code Playgroud)

我想将'data'附加到磁盘上的文件中.

我查看了NSData文档.有一些函数可以写入文件,但我看不到要附加到现有文件的任何内容.

有谁知道如何追加这些数据?任何帮助非常感谢!缺口.

append objective-c nsdata

18
推荐指数
1
解决办法
1万
查看次数

存档数据时发送到实例的无法识别的选择器(NSCoding)

-(void)transformObjects:(NSMutableArray*)array key:(NSString*)key
{
    NSMutableArray* archiveArray = [[NSMutableArray alloc]initWithCapacity:array.count];

    for (Furniture *furniture in array) {

        // The error occurs on the line below
        NSData *furnitureEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:furniture];
        [archiveArray addObject:furnitureEncodedObject];
    }

    NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
    [userData setObject:archiveArray forKey:key];
}
Run Code Online (Sandbox Code Playgroud)

错误日志:

2014-03-04 10:55:27.881 AppName[10641:60b] -[Furniture encodeWithCoder:]: unrecognized selector sent to instance 0x15d43350
Run Code Online (Sandbox Code Playgroud)

我不知道为什么在尝试存档对象时会出现"无法识别的选择器发送到实例".

nsdata ios unrecognized-selector

18
推荐指数
3
解决办法
2万
查看次数