2013-02-13 62 views
0

我已经检查了很多关于从我的服务器下载plist文件并将其转换为可用的NSDictionary的问题。无论出于何种原因,当我NSLog从plist文件创建的NSDictionary,我得到这个在控制台:下载plist作为NSDictionary返回(null)

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文件被缩小的事实有任何关系吗?

UPDATE:

获取文件用下面的代码:

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> 

等等等等等等等等等等...... 这一切都没有

+0

验证你的plist plist是一个有效的plist吗?此外,你可以尝试获取NSData的文件,将其转换为字符串,NSLog它(查看内容),并从NSData创建一个字典 – Ondrej 2013-02-13 16:53:12

+0

这就是事情。我在XCode中创建了一个模板plist,并用它在我的服务器上创建了一个PHP中的新plist,所以我认为它是有效的... – David 2013-02-13 16:55:41

+0

尝试将它下载为NSData并将其转换为字符串以查看内容,通常这只是一个令人讨厌的愚蠢像一个URL或类似的问题,让我知道,如果你得到任何数据回... – Ondrej 2013-02-13 16:58:28

回答

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

+0

只是想指出,实际上:) ... plist是无效的,这个答案是正确的! – Ondrej 2013-02-13 17:11:49

+0

万岁!它正在工作!谢谢! – David 2013-02-13 17:24:44