下载一个plist作为NSDictionary返回(null)

时间:2013-02-13 16:48:59

标签: iphone plist nsdictionary

我在这里检查了许多关于从我的服务器下载plist文件并将其转换为可用的NSDictionary的问题。无论出于何种原因,当我NSLog从plist文件创建NSDictionary时,我会在控制台中看到它:

Timestamp: (null)

我知道这是一个真实的文件:http://www.faithlifefellowship.us/iOS/sermons.plist

代码:

NSDictionary* plist = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://www.faithlifefellowship.us/iOS/sermons.plist"]];

NSLog(@"%@",plist);

有什么问题?

这与plist文件缩小的事实有什么关系吗?

更新

使用以下代码获取文件:

NSString* pl = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.faithlifefellowship.us/iOS/sermons.plist"] encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@",pl);

返回:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>1</key><array><integer>1</integer><string>I've Been Redeemed</string><string>Ive Been Redeemed</string><array><string></string></array><array><string>03/20/09</string><string>04/02/09</string><string>04/10/09</string><string>04/24/09</string><string>05/13/09</string><string>05/23/09</string><string>06/06/09</string><string>06/13/09</string><string>06/20/09</string></array><string>http://faithlifefellowship.us/Sermons/Banners/Banner-IBR.png</string><integer>9</integer><array><string>Many Christians allow things in their lives, not realizing that they have been redeemed from them and because of ignorance of God's Word, do not walk in total freedom. This series will set you free in every area of your life!</string><string>It is important to be able to differentiate between a curse in our lives and suffering tribulation for the Lord's sake.</string><string>The same exact blessing that was on Abraham's life is now on our lives because of Jesus Christ. Don't let ignorance or unbelief keep you from all that God has for you!</string><string>Well, I guess God wants me to be sick to teach me a lesson, ever heard someone say that? Well that does not work EVER in the light of God's Word. Gal 3:13 Christ HAS redeemed us...</string><string>As we age the world says that we get weaker mentally, but God's Word does not agree with this. Part of what we have been redeemed from is an unsound and unstable mind.</string><string>Get up early, work late, still nothing ever seems to go your way why? Becasue of the curse. The Good News is that we have been redeemed from loss and failure in our lives!</string>

Blah等等等等...... 一切都在那里

1 个答案:

答案 0 :(得分:4)

此文件中有未转义的&符号(即“Pt 1 & 2”),受限制。您应该在服务器上用&amp;替换它们,或者将文件的内容下载到NSString,然后用&替换&amp;

NSString *error;
NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.faithlifefellowship.us/iOS/sermons.plist"]
                                         encoding:NSUTF8StringEncoding
                                            error:&error];

str = [str stringByReplacingOccurrencesOfString:@" & " withString:@" &amp; "];
NSData *plistData = [str dataUsingEncoding:NSUTF8StringEncoding];

NSPropertyListFormat format;
NSDictionary *plist = [NSPropertyListSerialization propertyListFromData:plistData
                                                       mutabilityOption:NSPropertyListImmutable 
                                                                 format:&format 
                                                       errorDescription:&error];

另外,标签匹配存在很大问题。您可以使用http://www.xmlvalidation.com

验证您的plist